【问题标题】:Boost property tree xml parsing No such node ()Boost属性树xml解析No such node()
【发布时间】:2016-07-23 12:04:53
【问题描述】:

我正在尝试使用 boost/propert_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 >' 的实例后调用终止 what(): 没有这样的节点 (ax:hello) 中止(核心转储)`

如您所见,ax:hello标签已正确打开和关闭,所以尽管有属性,它应该能够找到它,对吧?

希望有人知道这里发生了什么!

【问题讨论】:

    标签: c++ xml boost boost-propertytree


    【解决方案1】:

    你在做其他错误/不同的事情:

    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'
    

    【讨论】:

    • 你知道为什么在传递带有文件而不是 istringstream 的 istream 时它不起作用吗?你能解释一下autodump 函数中的作用吗?会是哪一种?
    • 没有区别。显然我在本地使用了你的代码+文件。转储函数仅用于演示,使用 c++11
    • 是的,或者至少我做错了,这就是我发布问题的原因。当我从文件中执行此操作时,它没有找到任何节点。
    • 是的。我向你展示了这一点。如果你不想听,没关系。寻找其他差异。错误的文件,错误的路径,错误的编码,你知道的。调试它。
    • 我做到了,这就是我在这里问这个问题的原因。您的代码将 input.xml 的内容直接保存在 istringstream 中,而不是将文件加载到 ifstream 中。仍然感谢您的回答,因为我能够继续前进。
    猜你喜欢
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多