How to store the node of an xml file in a string a

2019-09-20 14:20发布

问题:

I have an xml file that looks like this :-

Disp.xml:

<Car>
<Car1>
    <Feature1>50</Feature1>
    <Feature2>100</Feature2>
    <Feature3>10</Feature3>
</Car1>
<Car2>
    <Feature1>100</Feature1>
    <Feature2>100</Feature2>
    <Feature3>10</Feature3>
</Car2>
<Car3>
    <Feature1>500</Feature1>
    <Feature2>0</Feature2>
    <Feature3>10</Feature3>
</Car3>
<Car4>
    <Feature1>1000</Feature1>
    <Feature2>0</Feature2>
    <Feature3>0</Feature3>
</Car4>
</Car>

Code for Xml reading :-

XmlDocument^ xDoc = gcnew XmlDocument();
String^ XPath =  ---path of file
xDoc->Load(XPath);
XmlNodeList^ nodes1 = xDoc->GetElementsByTagName("Car");
XmlNodeList^ car1 = xDoc->GetElementsByTagName("Car1");
XmlNodeList^ car1 = xDoc->GetElementsByTagName("Car2");
 for each (XmlNode^ node1 in nodes1)
 {          
    for each(XmlNode^ca1 in car1)
    {
        feature1= ca1->ChildNodes[1]->ChildNodes[0]->Value->ToString(); 
        feature2= ca1->ChildNodes[2]->ChildNodes[0]->Value->ToString(); 
        feature3= ca1->ChildNodes[3]->ChildNodes[0]->Value->ToString(); 
    }
}

I do have a structure

Example :-

struct Car1  
{
std::string feature1;
std::string feature2;
std::string feature3;
}

Similarly I have other structure too.

I need to read the nodes(values) of each car and store it in Car1,Car2,Car3,Car4 string array ,these string values must be later stored in individual structure in C++.

I have to do all this within a C++ Class Library .

A lot of research did let me find out only read xml data but not to store in string array and later in individual structure and that too in C++.

回答1:

My favourite tool for reading XML in C++ http://pugixml.org/

Just read the values using that (or another) library and then save them to your struct