10

Firstly I would like to say that I have been using an XML parser written by Frank Vanden Berghen and recently trying to migrate to Pugixml. I am finding the transition bit difficult. Hoping to get some help here.

Question: How can I build a tree from scratch for the small xml below using pugixml APIs? I tried looking into the examples on the pugixml home page, but most of them are hard coded with root node values. what I mean is

if (!doc.load("<node id='123'>text</node><!-- comment -->", pugi::parse_default | pugi::parse_comments)) return -1;

is hard-coded. Also I tried reading about xml_document and xml_node documentation but could not figure out how to start with if I have to build a tree from scratch.

#include "pugixml.hpp"

#include <string.h>
#include <iostream>

int main()
{
    pugi::xml_document doc;
    if (!doc.load("<node id='123'>text</node><!-- comment -->", pugi::parse_default | pugi::parse_comments)) return -1;

    //[code_modify_base_node
    pugi::xml_node node = doc.child("node");

    // change node name
    std::cout << node.set_name("notnode");
    std::cout << ", new node name: " << node.name() << std::endl;

    // change comment text
    std::cout << doc.last_child().set_value("useless comment");
    std::cout << ", new comment text: " << doc.last_child().value() << std::endl;

    // we can't change value of the element or name of the comment
    std::cout << node.set_value("1") << ", " << doc.last_child().set_name("2") << std::endl;
    //]

    //[code_modify_base_attr
    pugi::xml_attribute attr = node.attribute("id");

    // change attribute name/value
    std::cout << attr.set_name("key") << ", " << attr.set_value("345");
    std::cout << ", new attribute: " << attr.name() << "=" << attr.value() << std::endl;

    // we can use numbers or booleans
    attr.set_value(1.234);
    std::cout << "new attribute value: " << attr.value() << std::endl;

    // we can also use assignment operators for more concise code
    attr = true;
    std::cout << "final attribute value: " << attr.value() << std::endl;
    //]
}

// vim:et

XML:

<?xml version="1.0" encoding="UTF-8"?>
<d:testrequest xmlns:d="DAV:" xmlns:o="urn:example.com:testdrive">
   <d:basicsearch>
      <d:select>
         <d:prop>
            <o:versionnumber/>
            <d:creationdate />
         </d:prop>
      </d:select>
      <d:from>
         <d:scope>
            <d:href>/</d:href>
            <d:depth>infinity</d:depth>
         </d:scope>
      </d:from>
      <d:where>
         <d:like>
            <d:prop>
               <o:name />
            </d:prop>
            <d:literal>%img%</d:literal>
         </d:like>
      </d:where>
    </d:basicsearch>
</d:testrequest>

I could see most of the examples posted on how to read/parse the xml, but I could not find how to create one from the scratch.

Sandeep
  • 1,157
  • 1
  • 10
  • 24

2 Answers2

10

Please refer to the following section of the manual https://github.com/zeux/pugixml/blob/master/docs/manual.html#manual.modify.add and to the following sample code https://github.com/zeux/pugixml/blob/master/docs/samples/modify_add.cpp

starturtle
  • 616
  • 7
  • 23
zeuxcg
  • 8,703
  • 1
  • 22
  • 33
9

Home page of pugixml gives sample code for building XML tree from scratch.

Summary: Use default constructor for pugi::xml_document doc, then append_child for the root node. Generally, a node is first inserted. The insertion call's return value then serves as a handle for filling the XML node.

Constructing xml tree

anatolyg
  • 23,079
  • 7
  • 51
  • 113
Sandeep
  • 1,157
  • 1
  • 10
  • 24
  • For future reference: The example moved to [Github](https://github.com/zeux/pugixml/blob/master/docs/samples/modify_add.cpp). *Short*: use default constructor for `doc`, then `append_child` for the root node. Generally, a node is first inserted. The insertion call's return value then serves as a handle for filling the XML node. – starturtle Jul 14 '16 at 07:45