2016-07-23 11 views
1

私はboost/propert_treeライブラリを使用してXMLファイルを解析しようとしています。私はxmlファイルを正しく、すべて取得できますが、私が子を探すときには何も見つかりません。Boostプロパティツリーxmlの解析そのようなノードがありません

私はinput.xmlにファイルがあります:私は、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> 

機能:

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; 
} 

エラーを私が得るメッセージは:

'後押し:: exception_detail :: clone_impl>' 何()のインスタンス投げた後に呼び出さ終了:あなたが見ることができるように 中止(コアダンプ) `

:いいえ、そのようなノード(ハロー斧) ax:helloタグが正しく開閉されているので、属性にもかかわらずそれを見つけることができるはずです。

誰かがここで何が起こっているのか知りたいですね!

答えて

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 + " "); 
    } 
} 

プリントLive On Coliruすぎ

ax:hello 
    <xmlattr> 
     someatribute:ax: 'dwadawfesfjsefs' 
    something: '523523' 
    ax:whatever 
     ax:service_tree 
      ax:service: 'some' 
      ax:url: 'someulr' 
+0

ファイルの代わりに、istringstreamではistreamを渡すときに、それが動作しない理由を知っていますか? 'auto'が' dump'関数で何をするのかを説明できますか?どちらのタイプですか? – lpares12

+0

違いはありません。明らかに私はコード+ファイルをローカルで使用しました。ダンプ機能はデモ用であり、C++を使用しています11 – sehe

+0

はい、または少なくとも私は間違っているので、質問を投稿しました。私はファイルからノードを見つけません。 – lpares12

関連する問題