<localinfo>
<player_info>
<userInfo account="1990wyb" isLastLogin="0" isSave="0" pass="">1</userInfo>
<userInfo account="tt01234" isLastLogin="1" isSave="1" pass="272E7B">2</userInfo>
<userInfo account="zhltang" isLastLogin="0" isSave="1" pass="2D1A3A">3</userInfo>
<userInfo account="abest05" isLastLogin="0" isSave="1" pass="7BE688">4</userInfo>
</player_info>
</localinfo>
#include <iostream>
using std::cout;
using std::endl;
using std::string;
#include <boost/property_tree/ptree.hpp>//ptree
using boost::property_tree::ptree;
#include <boost/property_tree/xml_parser.hpp>//read,write
#include <boost/foreach.hpp>//BOOST_FOREACH
ptree pt;
/*读取,去掉空格*/
read_xml("info.xml", pt, boost::property_tree::xml_parser::trim_whitespace, std::locale());
BOOST_FOREACH(ptree::value_type &child, pt.get_child("localinfo"))
{
if ("<xmlattr>" == child.first)
{
cout << child.first << endl;
}
else if ("<xmlcomment>" == child.first)
{
cout << child.first << endl;
}
else if ("player_info" == child.first)
{
cout << child.first << endl;
BOOST_FOREACH(ptree::value_type &node, child.second)
{
if ("<xmlattr>" == node.first)
{
cout << "\t" << node.first << endl;
}
else if ("<xmlcomment>" == node.first)
{
cout << "\t" << node.first << endl;
}
else if ("userInfo" == node.first)
{
cout << "\t" << node.first << endl;//节点名
cout << "\t\t" << node.second.data() << endl;//节点值
cout << "\t\t" << node.second.get<string>("<xmlattr>.account") << endl;//节点属性
cout << "\t\t" << node.second.get<int>("<xmlattr>.isLastLogin") << endl;
cout << "\t\t" << node.second.get<int>("<xmlattr>.isSave") << endl;
cout << "\t\t" << node.second.get<string>("<xmlattr>.pass") << endl;
}
}
}
}
运行结果: