1. preparations:
Environment: Visual Studio 2010/Boost: boost_000054_0
Do not repeat what to create an application, but note that if you need to reference the Lib in the boost library, you must add the boost path to include directories in the VC ++ directories of properties.
XML file to be parsed:
Text. xml
<Students> <student ID = "1"> <Name> Zhang San </Name> <age> 18 </age> <sex> male </sex> </student> <student ID = "2"> <Name> Li Juan </Name> <age> 22 </age> <sex> female </sex> </student> <student ID =" 3 "> <Name> Wang Qiang </Name> <age> 21 </age> <sex> male </sex> </student> <student ID =" 4 "> <name> Li Si </Name> <age> 25 </age> <sex> male </sex> </student> <student ID = "5"> <Name> Yao na </Name> <age> 19 </age> <sex> female </sex> </student> <student ID = "6"> <Name> Cheng le </Name> <age> 21 </age> <sex> male </sex> </student> </students>
Entire program:
?
<br>#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> #include <boost/typeof/typeof.hpp>#include <iostream>#include <exception> |
Int main ()
{
Using boost: property_tree: ptree;
Ptree pt;
Try {
Read_xml ("text. xml", pt );
Boost_auto (child, Pt. get_child ("Students "));
For (boost_auto (Pos, child. Begin (); pos! = Child. End (); ++ POS)
STD: cout <pos-> second. get <int> ("<xmlattr>. ID ") <" <pos-> second. get <STD: String> ("name") <"" <pos-> second. get <STD: String> ("sex") <STD: Endl;
// System ("pause"); used for testing, you can temporarily stop the program here
}
Catch (STD: exception & E)
{
STD: cout <"error:" <E. What () <STD: Endl;
Return-1;
}
}
Result:
2. Resolution Process
A. Read XML files
?
readxml("text.xml");// Note: this is the default text. the XML file is stored in the root directory of the project file, that is, it is in the same directory as the main program file on the hard disk. Of course, it can also be stored in other directories, but don't forget that the double slash is located in the header file # include <boost/property_tree/xml_parser.hpp> </span> |
B. Obtain the subnode
?
BOOST_AUTO(child,pt.get_child("students"));// Note: The boost_auto here is essentially a macro. In the header file # include <boost/typeof. HPP> is defined in. To put it bluntly, it is the value assignment operation. It can be said that it is the boost version of auto in C ++ 11. Here, a variable child is defined and PT is added. the value of get_child ("Students") is assigned to child. <br> the reason why boost_auto is used is actually a generalization of the data type, so that users do not need to go into PT. the get_child ("Students") type focuses on its operations. For details, see C ++. Auto (with template) in section 11 <br> located in the header file # include <boost/typeof. HPP> </span> |
C. Read attributes or node content
?
for(BOOST_AUTO(pos,child.begin());pos!=child.end();++pos) // Now we can see the benefits of boost_auto. </Span> <br> {
std::cout<<pos->second.get<int>("<xmlattr>.id")<<" "<<pos->second.get<std::string>("name")<<" "<<pos->second.get<std::string>("sex")<<std::endl; } |
Pos here is the node iterator. In this article, student nodes are traversed. Here you can set pos (iterator)-> second. get <t (type)> (PATH) is a fixed method for parsing. Note that the path here is directly separated by vertices when you get the node content, for example, Path = "students. Student..."; if you want to evaluate the attribute, add <xmlattr>