如何将xml文件的节点存储在字符串数组中,然后存储到C ++中的结构 [英] How to store the node of an xml file in a string array and then to a structure in C++

查看:607
本文介绍了如何将xml文件的节点存储在字符串数组中,然后存储到C ++中的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似如下的xml文件: -



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>

Xml阅读代码:

  XmlDocument ^ xDoc = gcnew XmlDocument(); 
String ^ XPath = ---文件路径
xDoc-> Load(XPath);
XmlNodeList ^ nodes1 = xDoc-> GetElementsByTagName(Car);
XmlNodeList ^ car1 = xDoc-> GetElementsByTagName(Car1);
XmlNodeList ^ car1 = xDoc-> GetElementsByTagName(Car2);
每个(节点1中的XmlNode ^ node1)
{
每个(XmlNode ^ car1中的ca1)
{
feature1 = ca1-> ChildNodes [1] - > ChildNodes [0] - > Value-> ToString();
feature2 = ca1-> ChildNodes [2] - > ChildNodes [0] - > Value-> ToString();
feature3 = ca1-> ChildNodes [3] - > ChildNodes [0] - > Value-> ToString();
}
}

我有一个结构



示例: -

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

同样,我也有其他结构。



我需要读取每个汽车的节点(值)并将其存储在Car1,Car2,Car3,Car4字符串数组中,这些字符串值必须稍后存储在C ++的单个结构中。



我必须在C ++类库中做所有这些。



很多研究确实让我找到只读的xml数据但不是存储在字符串数组和以后的个人结构和C ++中。

解决方案

我最喜欢的在C ++中阅读XML的工具 http://pugixml.org/



只需使用(或另一个)库读取值,然后将它们保存到struct


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

解决方案

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

这篇关于如何将xml文件的节点存储在字符串数组中,然后存储到C ++中的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆