我正在尝试使用boost / properties_tree库解析XML文件。我可以正确地获取xml文件和所有内容,但是当我查找孩子时,它找不到任何内容。
我有一个input.xml文件:
<ax:hello someatribute:ax="dwadawfesfjsefs">
<something>523523</something>
<ax:whatever>
<ax:service_tree>
<ax:service>some</ax:service>
<ax:url>someulr</ax:url>
</ax:service_tree>
</ax:whatever>
</ax:hello>
我尝试解析xml的函数:
void parseXml(std::istream &stream)
{
using boost::property_tree::ptree;
ptree pt;
read_xml(stream, pt);
BOOST_FOREACH(ptree::value_type const &value, pt.get_child("ax:hello"))
{
std::cout << value.first;
}
}
主要功能:
int main()
{
std::ifstream stream("input.xml");
parseXml(stream);
return 0;
}
我得到的错误信息是:
在抛出'boost :: exception_detail :: clone_impl&gt;'的实例后终止调用 what():没有这样的节点(ax:你好) 中止(核心倾销)`
正如您所看到的,ax:hello
标记已正确打开和关闭,因此它应该能够找到它而不管属性,对吗?
希望有人知道这里发生了什么!
答案 0 :(得分:2)
您正在做其他错误/不同的事情:
<强> Live On Coliru 强>
#include <iostream>
#include <fstream>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
void parseXml(std::istream &stream)
{
using boost::property_tree::ptree;
ptree pt;
read_xml(stream, pt);
BOOST_FOREACH(ptree::value_type const &value, pt.get_child("ax:hello"))
{
std::cout << value.first << "\n";
}
}
int main()
{
std::istringstream stream(R"(
<ax:hello someatribute:ax="dwadawfesfjsefs">
<something>523523</something>
<ax:whatever>
<ax:service_tree>
<ax:service>some</ax:service>
<ax:url>someulr</ax:url>
</ax:service_tree>
</ax:whatever>
</ax:hello>
)");
parseXml(stream);
}
打印
<xmlattr>
something
ax:whatever
稍微复杂的倾销:
void dump(ptree const& pt, std::string const& indent = "") {
for (auto& node : pt) {
std::cout << indent << node.first;
auto value = boost::trim_copy(node.second.get_value(""));
if (!value.empty())
std::cout << ": '" << value << "'";
std::cout << "\n";
dump(node.second, indent + " ");
}
}
ax:hello
<xmlattr>
someatribute:ax: 'dwadawfesfjsefs'
something: '523523'
ax:whatever
ax:service_tree
ax:service: 'some'
ax:url: 'someulr'