1

My Problem is I am having a large xml file which I have to parse in C++. File is b.xml and I have to get message subtag from each tag. My first part of question is: https://stackoverflow.com/questions/27977851/parsing-xml-file-with-boost-c/27978152#27978152

<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

Community
  • 1
  • 1
avinashse
  • 1,440
  • 4
  • 30
  • 55
  • @EdHeal Please check the link which I have give instead of down-voting, I tried to get first part and get help to solve it, Now I asked there second part then I got answer to post this as a new question, that is why I am posting this as a seperate question. – avinashse Jan 16 '15 at 07:26
  • 1
    Question content should be in the question. – sehe Jan 16 '15 at 07:34

1 Answers1

2

I suggest you using an XML parser to parse this. Doing this kind of query on Property Trees is unwieldy in my experience.

If you use XPath, you can simply do what you want here in a single line. E.g. using PugiXML¹:

#include <pugixml.hpp>
#include <iostream>

int main() {
    pugi::xml_document doc;
    doc.load_file("input.xml");

    for (auto& n : doc.select_nodes("//root/MultiMessage/Message/structure/message/@value"))
        std::cout << n.attribute().value() << "\n";
}

prints

092374nmkla90u345n09832ljkn0a9845jhklasdf09u8426ljakljdgf09845u6kljsdgp89u45ljsdfp9gu4569078ljk
092374nmkla90u345n09832ljkn0a9845jhklasdf09u8426ljakljdgf09845u6kljsdgp89u45ljsdfp9gu4569078ljk
092374nmkla90u345n09832ljkn0a9845jhklasdf09u8426ljakljdgf09845u6kljsdgp89u45ljsdfp9gu4569078ljk

Alternatively, use an external tool like xmlstarlet or xmllint --xpath

¹ which can be used header-only, like boost property-tree

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank You for the reply, I downloaded PugiXML Library for linux and I extracted it. I am using eclipse, and I am adding library path to the pugixml path, but eclipse is not able to detect the path, On searching in google, I am getting library settings for visual studio only not for eclipse, please share your words in this regards. – avinashse Jan 16 '15 at 11:45
  • I use make files. On debian, `apt-get install libpugixml-dev` and then `g++ -std=c++11 test.cpp -lpugixml` does the job. Of course you can download/install manually, in which case the usual flurry of `./configure && make install` or [alternatively including the files into your project and **defining `PUGIXML_HEADER_ONLY`**](http://pugixml.googlecode.com/svn/tags/latest/docs/manual/install.html#manual.install.building.header) should do the job – sehe Jan 16 '15 at 12:36