C++????XML???
???????????? ???????[ 2016/12/22 13:35:34 ] ??????????????????? C++
????????????VRP??????????????????Xml????У???????????C++??д???????????C++?????Xml?????
????????????“C++???Xml???”?????????????????????????tinyXml?????????????????????????tinyXML?????XML?????
??????Щ???????ο?????@marchtea??????C++???XML??tinyXml????á???http://www.cnblogs.com/marchtea/archive/2012/11/08/2760593.html??
????tinyXml????????????C++??????????????????https://sourceforge.net/projects/tinyxml/??
???????????????????????????????Щ?????
??????????windows????VS??дC++???????@marchtea???????????????tinyxml.sln???????????????????????
??????tinystr.cpp??tinyxml.cpp??tinyxmlerror.cpp??tinyxmlparser.cpp??tinystr.h??tinyxml.h?????????????£?
???????????????????#include"tinystr.h"#include"tinyxml.h"??
???????????????????????VRP?????е?solomonbenchmark?????????Щ????????ο???tinyXml????????????????????????"doc"??????У????????????????"tutorial0"??html????????????????????????
????OK??nowbegins??
????????????Xml????????μ???????о???????
<!--??????Xml???-->
<?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
<instance>
<network>
<nodes>
<nodeid="0"type="0">
<cx>40.0</cx>
<cy>50.0</cy>
</node>
<!--??N+1????????node???-->
</nodes>
</network>
<requests>
<requestid="1"node="1">
<tw>
<start>145</start>
<end>175</end>
</tw>
<quantity>20.0</quantity>
<service_time>10.0</service_time>
</request>
<!--??N????????request???-->
</requests>
</instance>
?????????????????????nodes??????????requests????1???????????nodes????????????N??????????1????????requests?????????????
?????????xml????е???Щ?????????????????У???????????????????????????£?
//Customer.h
#ifndef_Customer_H
#define_Customer_H
classCustomer{
public:
Customer(intid=0??floatx=0??floaty=0??floatstartTime=0??floatendTime=0??floatquantity=0??floatserviceTime=0);
voidsetId(intid);//???ó??id???
voidsetX(floatx);//???ó??x???
voidsetY(floaty);//???ó??y???
voidsetStartTime(floatstartTime);//???ó??startTime???
voidsetEndTime(floatendTime);//???ó??endTime???
voidsetQuantity(floatquantity);//???ó??quantity???
voidsetServiceTime(floatserviceTime);//???ó??serviceTime???
voidshow();//???????????
private:
intid;
floatx;
floaty;
floatstartTime;
floatendTime;
floatquantity;
floatserviceTime;
};
#endif
????OK????????????????main.cpp????Customer.cpp?????????????
//main.cpp
#include"Customer.h"
#include"tinystr.h"
#include"tinyxml.h"
#include<iostream>
#include<vector>
#include<string>
#include<stdlib.h>
#include<iomanip>
usingnamespacestd;
staticconstintNUM_OF_CUSTOMER=51;//???????
staticconstchar*FILENAME="RC101_050.xml";//?????
intmain(){
vector<Customer*>customerSet(0);//??????????????Customer????????
inti??j??k??count;
inttemp1;//???????????
floattemp2;//????????????
Customer*customer;//???????????
for(i=0;i<NUM_OF_CUSTOMER;i++){//??????????
customer=newCustomer();
customerSet.push_back(customer);
}
TiXmlDocumentdoc(FILENAME);//????XML???
if(!doc.LoadFile())return-1;//??????????????????
TiXmlHandlehDoc(&doc);//hDoc??&doc???????
TiXmlElement*pElem;//??????????
pElem=hDoc.FirstChildElement().Element();//???????
TiXmlHandlehRoot(pElem);//hRoot??????
//???x??y?????????network->nodes->node?????
TiXmlElement*nodeElem=hRoot.FirstChild("network").FirstChild("nodes").FirstChild("node").Element();//????????node???
count=0;//?????????????node?????????node?????????????????customer??
for(nodeElem;nodeElem;nodeElem=nodeElem->NextSiblingElement()){//???????node???????
customer=customerSet[count];//?????????????????????μ????????????????????
TiXmlHandlenode(nodeElem);//nodeElem????????
TiXmlElement*xElem=node.FirstChild("cx").Element();//cx???
TiXmlElement*yElem=node.FirstChild("cy").Element();//cy???
nodeElem->QueryIntAttribute("id"??&temp1);//??id???temp1?У??????????
customer->setId(temp1);
temp2=atof(xElem->GetText());//char?float
customer->setX(temp2);
temp2=atof(yElem->GetText());
customer->setY(temp2);
count++;
}
//??????????
TiXmlElement*requestElem=hRoot.FirstChild("requests").FirstChild("request").Element();//?????request???
count=1;
for(requestElem;requestElem;requestElem=requestElem->NextSiblingElement()){
customer=customerSet[count];//?????????????????????μ????????????????????
TiXmlHandlerequest(requestElem);//??????????
TiXmlElement*startTimeElem=request.FirstChild("tw").FirstChild("start").Element();//starttime
TiXmlElement*endTimeElem=request.FirstChild("tw").FirstChild("end").Element();//endtime
TiXmlElement*quantityElem=request.FirstChild("quantity").Element();//quantity
TiXmlElement*serviceTimeElem=request.FirstChild("service_time").Element();//servicetime
//?????????????
temp2=atof(startTimeElem->GetText());
customer->setStartTime(temp2);
temp2=atof(endTimeElem->GetText());
customer->setEndTime(temp2);
temp2=atof(quantityElem->GetText());
customer->setQuantity(temp2);
temp2=atof(serviceTimeElem->GetText());
customer->setServiceTime(temp2);
count++;
}
//??????????????????????
cout<<setiosflags(ios_base::left)<<setw(6)<<"id"<<setw(6)<<"x"<<setw(6)<<
"y"<<setw(12)<<"startTime"<<setw(12)<<"endTime"<<setw(12)<<"quantity"<<setw(14)<<"serviceTime"<<endl;
for(i=0;i<NUM_OF_CUSTOMER;i++){
customer=customerSet[i];
customer->show();
}
system("pause");
return0;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11