vs2017、C++、读取XML文件,并获取里面的节点值

更新时间:2023-07-29 15:24:54 阅读: 评论:0

海参蛋羹
vs2017、C++、读取XML⽂件,并获取⾥⾯的节点值
新⼿⼩⽩,不对的请各位前辈多多指教!
这篇⽂章是记录:使⽤vs2017,C++语⾔,实现读取xml⽂本⽂件并保存XML节点信息时遇到的⼩问题。
前提:本⽂读取XML⽂件,将XML⽂件中的节点信息存储为数组,在使⽤节点信息的时候,遍历存储好的数组就可以了。
1.思考如何获取XML⽂件的属性值。
最开始我思考使⽤字符串分隔的⽅式去获取xml⽂件的节点信息,思路是这样的:先按⾏读取XML⽂件,获取⽂件的每⾏字符串,对字符串使⽤分割字符的⽅式进⾏处理。再将得到的字符串保存起来。但是⽤功能实现了⼀下,没成功,下⾯是我的代码以及结果。
#include <iostream>
#include  <string>
#include  <fstream>
#include <vector>
#include <sstream>
#include <casrt>
using namespace std;
void txt_to_vectordouble(vector<vector<string>>& res, string pathname)
{
//此⽅法是读取⽂本⽂件,按⾏读取⽂本⽂件,并将每⼀⾏的内容存储在vector⾥⾯,最后存储在⼀个⼤vector⾥⾯
ifstream infile;
int lineIndex=0;
infile.open(pathname.data());//将⽂件流对象与⽂件连接起来
asrt(infile.is_open());//asrt断⾔,符合条件则继续执⾏,不满⾜条件程序将终⽌
vector<string> suanz;
string s;
while (getline(infile, s)) {//按⾏读取
istringstream is(s);//将读出的⼀⾏转成数据流进⾏操作
string d;
while (!is.eof()) {//如果不是⽂件尾
is >> d;
suanz.push_back(d);//存储
}
lineIndex++;
res.push_back(suanz);//存储
suanz.clear();
s.clear();
}
cout << lineIndex << endl;
infile.clo();
}
int main() {
vector<vector<string>> data;
txt_to_vectordouble(data, "l"); for (int i = 0; i < data.size();i++) {
for (int j = 0; j < data[i].size();j++) {
cout << data[i][j];
}
飞机机身
cout << endl;
小水电站
}
system("chcp 65001");//处理中⽂乱码的 system("pau");
return 0;
}
读取的XML:
<?xml version="1.0" encoding="gb2312"?>
<ROOT>
<FeatruePoints>
<FeatruePoint name="A-MQ1-1" recognize="1"/>
<FeatruePoint name="B-MQ1-2" recognize="1"/>
<FeatruePoint name="B-MH10-2" recognize="1"/>
<FeatruePoint name="B-MH10-3" recognize="1"/>
<FeatruePoint name="B-MH10-4" recognize="1"/>
<FeatruePoint name="B-MH10-5" recognize="1"/>
</FeatruePoints>
常用法律知识
<WeldPoints>
<WeldPoint name="A-0-1" calculate="1" FeaturPoint1="A-MH-01"/>
<WeldPoint name="A-0-2" calculate="1" FeaturPoint1="A-MH-02"/>
<WeldPoint name="A-0-3" calculate="1" FeaturPoint1="A-MH-03"/>
<WeldPoint name="A-1-1" calculate="2" FeaturPoint1="A-MQ1-1" FeaturPoint2="A-MQ1-2" FeaturP
oint3="A-MH8-1" FeaturPoint4="A-MH3-1"/>    <WeldPoint name="A-1-2" calculate="2" FeaturPoint1="A-MQ1-1" FeaturPoint2="A-MQ1-2" FeaturPoint3="A-MH8-2" FeaturPoint4="A-MH10-4"/>    <WeldPoint name="A-1-3" calculate="2" FeaturPoint1="A-MQ1-3" FeaturPoint2="A-MQ1-4" FeaturPoint3="A-MH8-3" FeaturPoint4="A-MH10-5"/>    <WeldPoint name="A-2-1" calculate="2" FeaturPoint1="A-MQ2-1" FeaturPoint2="A-MQ2-2" FeaturPoint3="A-MH8-1" FeaturPoint4="A-MH3-1"/>    <WeldPoint name="A-2-2" calculate="2" FeaturPoint1="A-MQ2-1" FeaturPoint2="A-MQ2-2" FeaturPoint3="A-MH8-2" FeaturPoint4="A-MH10-4"/>    <WeldPoint name="A-2-3" calculate="2" FeaturPoint1="A-MQ2-3" FeaturPoint2="A-MQ2-4" FeaturPoint3="A-MH8-3" FeaturPoint4="A-MH10-5"/>  </WeldPoints>
<Roads>
<Road name="路径0,三点点焊">
<Point name="A-0-1"/>
<Point name="A-0-2"/>
<Point name="A-0-3"/>
</Road>
<Road name="路径1">
小砂锅
<Point name="A-1-1"/>
<Point name="A-1-2"/>
<Point name="A-1-3"/>
</Road>
<Road name="路径2">
<Point name="A-2-1"/>
东港政府网
<Point name="A-2-2"/>
<Point name="A-2-3"/>
</Road>
怎样去肝火最有效
</Roads>
<Schemes>
<Scheme name="点固">
<WRoad name="路径0"/>
<WRoad name="路径1"/>
<WRoad name="路径2"/>
</Scheme>
<Scheme name="划线">
<WRoad name="路径1"/>
<WRoad name="路径2"/>
</Scheme>
<Scheme name="点焊">
<WRoad name="路径1"/>
<WRoad name="路径2"/>
</Scheme>
<Scheme name="连续">
<WRoad name="路径1"/>
<WRoad name="路径2"/>
</Scheme>
</Schemes>
</ROOT>
运⾏结果:
中⽂乱码⼀直是⼼头病啊,没找到合适的处理⽅法。但是XML⽂件的内容都获取到了。
但是要使⽤字符串分隔就很⿇烦了,因为XML⾥⾯的数据是随时变化的,如果使⽤字符串分隔,移植效果不好,⽽且如果后期要增加XML ⽂件的⾏数或者⼀⾏的长度,程序会崩溃的。
后来发现使⽤字符串分割的⽅法并不好,就去搜了⼀些资料,后来发现使⽤外部资源类tinyXML处理XML更为合适,果断更换了实现思路。
下载后将tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparr.cpp这6个复制到⼯程⾥⾯。如图。别忘了程序的根⽬录⾥⾯也要加,我将这6个⽂件存放在readXML⽂件夹⾥,所以引⼊头⽂件的时候也要加上这个⽂件夹的名字。
#include "readXML/tinystr.h"
#include "readXML/tinyxml.h"
此时读取XML⽂件的思路就有啦。按照这个思路继续往下写。
2.第⼆个问题是:我存储数据的时候有的使⽤了两层结构体嵌套,两个结构体嵌套的时候不知道怎么向⾥⾯那层结构体赋值。后来去百度了以下,找到如下解决办法:
struct WeldPoints {//计算点结构体
string weldPointName;
int calculate;
struct FeaturePoint {//这⾥使⽤结构体嵌套
vector<string> featurePoint;
}featpoint;//必须声明⼀个结构体变量,不然下⾯程序没法访问到FeaturePoint⾥⾯的属性
};
struct Roads {
struct Road {//结构体嵌套
string roadName;
vector<string> pointName;
}road;//必须声明。解释同上
};
赋值:
struct WeldPoints welds;//创建结构体变量
泡茶流程图welds.featpoint.featurePoint.push_back (pWeld_FeaturPoint->Value());//赋值
struct Roads roads;//创建结构体变量
这样就赋值成功啦。
3.在使⽤tinyXML获取节点属性时,不知道如何获取兄弟节点,以及节点中的第⼆个属性。解决⽅法如下:
兄弟节点:NextSiblingElement();
TiXmlElement *WeldPoints = FeatruePoints->NextSiblingElement();//得到FeaturePoints的兄弟节点WeldPoints
cout << "⽂件中第⼆个要遍历的节点" << WeldPoints->Value() << endl;//输出WeldPoints
获取第⼆个属性:Next();

本文发布于:2023-07-29 15:24:54,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1100965.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:节点   字符串   结构
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图