Parse Xml File with C++

 ̄綄美尐妖づ 提交于 2019-12-11 23:27:30

问题


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


回答1:


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

  • What XML parser should I use in C++?
  • What is the best open XML parser for C++?

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



来源:https://stackoverflow.com/questions/27979067/parse-xml-file-with-c

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