-1

I'm very new to XML parse in C++. I want to parse the XML. So, I'm using the PugiXML library. I mainly want to get the values from each of the children nodes.

Here, is the sample code till here I have written, then I don't understand what to do next.

void XML()
{
    //Suppose I'm getting XML data in the form of a string, and store in string variable **xmlString**

    //Using 3rd party lib Pugixml parser want to store all the **xmlString data in doc**.
    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_string(xmlString.c_str());

    // I want value will store the data "Sheet1" as per the syntax. But somehow, doc store the value till here **<?xml version = "1.0" encoding = "UTF-8"?><Graphics**
    string  value = doc.child("Graphics").child("SheetInfo").attribute("SheetName").value();
}

Here is the sample XML data which is stored in the xmlString.

<?xml version = "1.0" encoding = "UTF-8"?>
<Graphics>
    <SheetInfo>
        <SheetName>Sheet1</SheetName>
        <Circle>
            <IGDSElement>
                <Type>89</Type>
            </IGDSElement>
            <cPt_x>0.212050</cPt_x>
            <cPt_y>0.148307</cPt_y>
            <orientation>0</orientation>
        </Circle>
    <SheetInfo>
<Graphics>

Please help me to parse this XML format data while going to each node.

1 Answers1

0

If you want to visit each node, you can traverse it with a for loop:

    for(const auto& child : doc.child("Graphics"))
        cout << child.child_value() << endl;

To find specific node you can use XPath. Here are examples on various things commonly needed for XML parsing.

Devolus
  • 21,661
  • 13
  • 66
  • 113