Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

How to store the node of an xml file in a string array and then to a structure in C++

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++.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...