【问题标题】:Parsing XML File with Boost C++使用 Boost C++ 解析 XML 文件
【发布时间】:2023-03-06 07:34:01
【问题描述】:

我必须使用 boost c++ 解析一个 xml 文件,我已经编写了一个适用于此 xml 的测试代码。 a.xml

<a>
    <modules>
        <module>abc</module>
        <module>def</module>
        <module>ghi</module>
    </modules>
</a>

输出来了

abc
def
ghi

但是对于这个a.xml 文件,我的测试代码没有显示任何输出,3 个空行作为输出。

<a>
    <modules>
        <module value = "abc"/>
        <module value = "def"/>
        <module value = "abc"/>
    </modules>
</a>

这里是测试代码:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <iostream>

int main()
{
    using boost::property_tree::ptree;
    ptree pt;
    read_xml("a.xml",pt);
    BOOST_FOREACH(ptree::value_type &v, pt.get_child("a.modules"))
        std::cout<<v.second.data()<<std::endl;
    return 0;
}

我的问题是我有一个大的 xml 文件,其中包含来自两个文件的混合模式,我必须解析它。 文件是 b.xml,我必须从每个标签中获取消息子标签。

<MultiMessage>
    <Message structID="1710" msgID="0" length="50">
        <structure type="AppHeader">
        </structure>
    </Message>
    <Message structID="27057" msgID="27266" length="315">
        <structure type="Container">
            <productID value="166"/>
            <publishTo value="xyz"/>
            <templateID value="97845"/>
            <sendingTime value="1421320622367060444"/>
            <message value="092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK"/>
        </structure>
    </Message>
</MultiMessage>
<MultiMessage>
    <Message structID="1710" msgID="0" length="50">
        <structure type="AppHeader">
        </structure>
    </Message>
    <Message structID="27057" msgID="27266" length="315">
        <structure type="Container">
            <productID value="166"/>
            <publishTo value="xyz"/>
            <templateID value="97845"/>
            <sendingTime value="1421320622367060444"/>
            <message value="092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK"/>
        </structure>
    </Message>
</MultiMessage>
<MultiMessage>
    <Message structID="1710" msgID="0" length="50">
        <structure type="AppHeader">
        </structure>
    </Message>
    <Message structID="27057" msgID="27266" length="315">
        <structure type="Container">
            <productID value="166"/>
            <publishTo value="xyz"/>
            <templateID value="97845"/>
            <sendingTime value="1421320622367060444"/>
            <message value="092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK"/>
        </structure>
    </Message>
</MultiMessage>

输出应该是:

092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK
092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK
092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK

谢谢

问候

【问题讨论】:

    标签: c++ boost xml-parsing boost-propertytree


    【解决方案1】:

    Boost Documentation:

    XML 元素的属性存储在 subkey 中。属性节点中的每个属性都有一个子节点。当没有属性时,节点的存在是不保证或必要的。

    <module value = "abc"/>
    //One way would be this:
    boost::get<std::string>("module.<xmlattr>.value");
    

    另一种方法(未经测试),似乎更好:

    BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("a.modules"))
    {
        std::cout << v.second.get_child("<xmlattr>.type").data() << std::endl;
        std::cout << v.second.get_child("<xmlattr>.Reference").data() << std::endl;
    }
    

    另外一张来自here.

    //Parse XML...
    BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("a.modules"))
    {
        const boost::property_tree::ptree &attributes = v.second.get_child("<xmlattr>", boost::property_tree::ptree());
        BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, attributes)
        {
            std::cout << v.first.data() << std::endl;
            std::cout << v.second.data() << std::endl;
        }
    }
    

    【讨论】:

    • 这行得通,但是我有一个不同的文件,我编辑了我的问题,我必须解析消息字段,该怎么做。
    • @avinashse 新问题应作为新问题提出。
    • @sehe 我发布了新问题http://stackoverflow.com/questions/27979067/parse-xml-file-with-c
    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 2013-05-29
    • 2015-03-14
    • 1970-01-01
    • 2021-05-23
    • 2013-07-22
    • 1970-01-01
    相关资源
    最近更新 更多