新手小白,请多多指教哦!
这篇文章就是写XML文件的完整版啦。上一篇文章是我在完成整个程序遇到的问题和解决办法。
文章目的:循环遍历vector数组,完成写XML文件
之前的两篇文章写了读取XML文件里面的节点信息,读取XML文件的时候是将XML文件节点信息存储在vector向量中,这篇文章是要遍历vector向量,循环取值写成XML文件。
这个是存储数据的vector向量
vector<FeaturePoints> featurepoints;//定义一个向量存储FeaturePoint
vector<WeldPoints> weldpoints;//使用向量存储第二个点信息
vector<Roads> vec_Roads;//定义vector向量用来存储Road
vector<Schemes> vec_Schemes;//定义vector存储Schemes
接下来就是循环遍历这四个向量,将他们存储的数据写成XML文件。
//写成XML文件
int writeXmlFile() {
TiXmlDocument *writeDoc = new TiXmlDocument;//xml文档指针
TiXmlDeclaration *decl = new TiXmlDeclaration("1.0","gb2312","");//文档格式声明
writeDoc->LinkEndChild(decl);//写入文档
TiXmlElement *RootElement = new TiXmlElement("COVER");//根元素
RootElement->SetAttribute("positiveX100_X","1.0");//为根元素创建属性
RootElement->SetAttribute("positiveX100_Y", "2.0");//SetAttribute的参数(const char * cname, const char * cvalue)
RootElement->SetAttribute("negativeX100_X", "3.0");//所以会涉及到参数转换问题
RootElement->SetAttribute("negativeX100_Y", "4.0");
RootElement->SetAttribute("positiveY100_X", "5.0");
RootElement->SetAttribute("positiveY100_Y", "6.0");
RootElement->SetAttribute("negativeY100_X", "7.0");
RootElement->SetAttribute("negativeY100_Y", "8.0");
RootElement->SetAttribute("homePoint_X", "9.0");
RootElement->SetAttribute("homePoint_Y", "10.0");
writeDoc->LinkEndChild(RootElement);//写入文档
TiXmlElement *RootFeatruePointsElement = new TiXmlElement("FeatruePoints");//第一个父节点
for (int j = 0; j < featurepoints.size();j++) {//循环遍历vector的个数
TiXmlElement *FeaturPointElement = new TiXmlElement("FeaturePoint");//父节点的第一个孩子节点
char * cha_PointName = (char *)featurepoints[j].featurePointName.c_str();//string转char *
char cha_recognize = (char )featurepoints[j].recognize;//int 转char *
FeaturPointElement->SetAttribute("name", cha_PointName);//设置属性
FeaturPointElement->SetAttribute("recognize", cha_recognize);
//下面这句话如果没加,上面设置的属性什么的就不会出现在XML文件里面
RootFeatruePointsElement->LinkEndChild(FeaturPointElement);
}
RootElement->LinkEndChild(RootFeatruePointsElement);//将第一个父节点加入到根节点里面
TiXmlElement *WeldPointsElement = new TiXmlElement("WeldPoints");//第二个父节点
for (int k = 0; k < weldpoints.size();k++) {//循环遍历第二个vector向量
TiXmlElement *weldPointElement = new TiXmlElement("WeldPoint");//父节点的子节点
char * char_weldPointName = (char *)weldpoints[k].weldpoint.WeldPointName.c_str();//string转char *
char cha_calcuate = (char )weldpoints[k].weldpoint.calcuate;//int转char *
weldPointElement->SetAttribute("name", char_weldPointName);//设置属性
weldPointElement->SetAttribute("calculate", cha_calcuate);
weldPointElement->SetAttribute("adjust_X", "0.0");
weldPointElement->SetAttribute("adjust_Y", "0.0");
weldPointElement->SetAttribute("adPoint0_X", "0.0");
weldPointElement->SetAttribute("adPoint0_Y", "0.0");
weldPointElement->SetAttribute("adPoint1_X", "0.0");
weldPointElement->SetAttribute("adPoint1_Y", "0.0");
for (int t = 0; t < weldpoints[k].weldpoint.FeaturPointName.size();t++) {//循环遍历name
TiXmlElement *FeturPointNameElement = new TiXmlElement("FeaturPoint");//子节点
char * cha_FeaturPointName = (char *)weldpoints[k].weldpoint.FeaturPointName[t].c_str();//string转char *
FeturPointNameElement->SetAttribute("name", cha_FeaturPointName);//设置属性
weldPointElement->LinkEndChild(FeturPointNameElement);//加到weldPointElement节点里面
}
WeldPointsElement->LinkEndChild(weldPointElement);//加到WeldPointsElement节点里面
}
RootElement->LinkEndChild(WeldPointsElement);//加到根节点
TiXmlElement *RoadsElement = new TiXmlElement("Roads");//第三个父节点
for (int e = 0; e < vec_Roads.size();e++) {//循环遍历第三个vector
TiXmlElement *RoadNameElement = new TiXmlElement("Road");//孩子节点
char * roadName = (char *)vec_Roads[e].road.roadName.c_str();//string转char *
RoadNameElement->SetAttribute("name", roadName);//设置属性
for (int r = 0; r < vec_Roads[e].road.pointName.size();r++) {//循环遍历取name
TiXmlElement *pointElement = new TiXmlElement("Point");//子节点Point
char * cha_pointName = (char *)vec_Roads[e].road.pointName[r].c_str();//string转char *
pointElement->SetAttribute("name", cha_pointName);//设置属性
RoadNameElement->LinkEndChild(pointElement);//将pointElement加到RoadNameElement
}
RoadsElement->LinkEndChild(RoadNameElement);//将RoadNameElement加到RoadsElement
}
RootElement->LinkEndChild(RoadsElement);//将父节点加到跟节点里面
TiXmlElement *SchemesElement = new TiXmlElement("Schemes");//第四个父节点
for (int w = 0; w < vec_Schemes.size();w++) {//循环遍历vector
TiXmlElement *SchemeElement = new TiXmlElement("Scheme");//父节点的孩子节点
char * cha_schemeName = (char *)vec_Schemes[w].scheme.schemeName.c_str();//string转char *
SchemeElement->SetAttribute("name",cha_schemeName);//设置属性
for (int a = 0; a < vec_Schemes[w].scheme.wRoadName.size();a++) {
TiXmlElement *WRoadElement = new TiXmlElement("WRoad");//孩子节点的孩子节点
char * cha_RoadName = (char *)vec_Schemes[w].scheme.wRoadName[a].c_str();//string转char *
WRoadElement->SetAttribute("name", cha_RoadName);//设置属性
SchemeElement->LinkEndChild(WRoadElement);//将设置好的属性连接到SchemeElement
}
SchemesElement->LinkEndChild(SchemeElement);//将SchemeElement加到SchemesElement
}
RootElement->LinkEndChild(SchemesElement);//将第四个父节点加到根节点里面
writeDoc->SaveFile("test.xml");//要保存的XML文件名字,默认和程序在一个目录里面
delete writeDoc;
return 1;
}
main入口
int main() {
//下面这句是为了处理中文乱码加的,但是刚加的确实可以处理中文乱码问题,
//后来不知道因为什么,不加这句话也好使了
//system("chcp 65001");
readXmlFile();//读文件
printf("\nafter write\n");
writeXmlFile();//写文件
system("pause");
return 0;
}
这个程序是和之前读取XML文件连在一起看的,先读取XML,Vector里面才会有数据,如果你自己往里面村其他的数据也是可以的,但是写文件的思想都是一样的。
运行结果:
<?xml version="1.0" encoding="gb2312" ?>
<COVER positiveX100_X="1.0" positiveX100_Y="2.0" negativeX100_X="3.0" negativeX100_Y="4.0" positiveY100_X="5.0" positiveY100_Y="6.0" negativeY100_X="7.0" negativeY100_Y="8.0" homePoint_X="9.0" homePoint_Y="10.0">
<FeatruePoints>
<FeaturePoint name="A-MQ1-1" recognize="1" />
<FeaturePoint name="A-MH10-5" recognize="1" />
</FeatruePoints>
<WeldPoints>
<WeldPoint name="A-12-1" calculate="1" adjust_X="0.0" adjust_Y="0.0" adPoint0_X="0.0" adPoint0_Y="0.0" adPoint1_X="0.0" adPoint1_Y="0.0">
<FeaturPoint name="A-MH8-3" />
</WeldPoint>
<WeldPoint name="A-12-2" calculate="1" adjust_X="0.0" adjust_Y="0.0" adPoint0_X="0.0" adPoint0_Y="0.0" adPoint1_X="0.0" adPoint1_Y="0.0">
<FeaturPoint name="A-MH10-5" />
</WeldPoint>
</WeldPoints>
<Roads>
<Road name="路径">
<Point name="3" />
<Point name="A4" />
</Road>
<Road name="路径12">
<Point name="A1" />
<Point name="A2" />
</Road>
</Roads>
<Schemes>
<Scheme name="A_DH">
<WRoad name="路径0" />
<WRoad name="路径1" />
</Scheme>
<Scheme name="A_LX">
<WRoad name="路径0" />
<WRoad name="路径1" />
<WRoad name="路径12" />
</Scheme>
</Schemes>
</COVER>
好啦就是这样啦!如有不对的请大神多多指教!!!