[英]Boost property tree xml parsing No such node ()
我正在嘗試使用boost / propert_tree庫解析XML文件。 我可以正確獲取xml文件以及所有內容,但是當我尋找child時,找不到任何文件。
我有一個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>'what()實例后調用終止方法:what():沒有這樣的節點(ax:hello)中止(核心已轉儲)`
如您所見, ax:hello
標記已正確打開和關閉,因此盡管具有屬性,也應該能夠找到它,對嗎?
希望有人知道這里發生了什么!
您正在做其他錯誤/不同的事情:
#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 + " ");
}
}
也可以在Coliru上實時打印
ax:hello
<xmlattr>
someatribute:ax: 'dwadawfesfjsefs'
something: '523523'
ax:whatever
ax:service_tree
ax:service: 'some'
ax:url: 'someulr'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.