Parsing XML File with Boost C++

2019-06-14 11:58发布

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条回答
We Are One
2楼-- · 2019-06-14 12:28

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;
    }
}
查看更多
登录 后发表回答