Parsing XML File with Boost C++

橙三吉。 提交于 2019-12-08 08:21:25

问题


I have to parse one xml file using boost c++, I have written one test code which is working for this xml. a.xml

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

Output is coming

abc
def
ghi

but for this a.xml file, my test code is not showing any output, 3 blank lines are coming as output.

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

here is the test code:

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

My Problem is I am having a large xml file which contains the mixture of patterns from both the files and I have to parse it. File is b.xml and I have to get message subtag from each tag.

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

and output should be :

092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK
092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK
092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK

Thank You

Regards


回答1:


Boost Documentation:

The attributes of an XML element are stored in the subkey . There is one child node per attribute in the attribute node. Existence of the node is not guaranteed or necessary when there are no attributes.

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

One more way (untested), which appears to be better:

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

One more taken from 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;
    }
}


来源:https://stackoverflow.com/questions/27977851/parsing-xml-file-with-boost-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!