解析XML文件升压C ++ [英] Parsing XML File with Boost C++
问题描述
我使用boost C ++来解析一个XML文件中,我写了一个测试code这是在为这个XML。 a.xml
< A>
<模块>
<&模块GT; ABC< /模块>
<&模块GT;&高清LT; /模块>
<&模块GT;&GHI LT; /模块>
< /模块>
&所述; / A>
输出来了
ABC
DEF
GHI
但对于本 a.xml
文件,我的测试code未显示任何输出,3个空行来作为输出。
< A>
<模块>
<模值=ABC/>
<模值=高清/>
<模值=ABC/>
< /模块>
&所述; / A>
下面是测试code:
的#include<升压/ property_tree / ptree.hpp>
#包括LT&;升压/ property_tree / xml_parser.hpp>
#包括LT&;升压/ foreach.hpp>
#包括LT&;&iostream的GT;诠释的main()
{
使用boost :: property_tree :: ptree中;
ptree中PT;
read_xml(a.xml,PT);
BOOST_FOREACH(ptree中:: VALUE_TYPE&安培; V,pt.get_child(a.modules))
性病::法院LT&;< v.second.data()<<的std :: ENDL;
返回0;
}
我的问题是我有其中包含来自两个文件模式的混合了大量的XML文件,我必须分析它。
文件是b.xml,我必须从每个标签中获取的信息的子标签。
< MultiMessage>
<消息structID =1710MSGID =0长度=50>
<结构类型=AppHeader>
< /结构>
< /信息>
<消息structID =27057MSGID =27266长度=315>
<结构类型=容器>
< ProductID值=166/>
< publishTo值=XYZ/>
< templateID值=97845/>
< sendingTime值=1421320622367060444/>
<消息value=\"092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK\"/>
< /结构>
< /信息>
< / MultiMessage>
< MultiMessage>
<消息structID =1710MSGID =0长度=50>
<结构类型=AppHeader>
< /结构>
< /信息>
<消息structID =27057MSGID =27266长度=315>
<结构类型=容器>
< ProductID值=166/>
< publishTo值=XYZ/>
< templateID值=97845/>
< sendingTime值=1421320622367060444/>
<消息value=\"092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK\"/>
< /结构>
< /信息>
< / MultiMessage>
< MultiMessage>
<消息structID =1710MSGID =0长度=50>
<结构类型=AppHeader>
< /结构>
< /信息>
<消息structID =27057MSGID =27266长度=315>
<结构类型=容器>
< ProductID值=166/>
< publishTo值=XYZ/>
< templateID值=97845/>
< sendingTime值=1421320622367060444/>
<消息value=\"092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK\"/>
< /结构>
< /信息>
< / MultiMessage>
和输出应该是:
<$p$p><$c$c>092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK
092374NMKLA90U345N09832LJKN0A9845JHKLASDF09U8426LJAKLJDGF09845U6KLJSDGP89U45LJSDFP9GU4569078LJK
感谢您
问候
<一个href=\"http://www.boost.org/doc/libs/1_44_0/doc/html/boost_propertytree/parsers.html#boost_propertytree.parsers.xml_parser\"相对=nofollow> Boost文档:
的XML元素的属性被存储在子项。有一个在属性节点每个属性一个子节点。节点的存在是不保证或必要的时候没有属性。
块引用>&LT;模值=ABC/&GT;
//一个办法是这样的:
提高::获得&LT;标准::字符串&GT;(模块&LT; xmlattr&GT; .value的);另一种方式(未经测试),这似乎是更好的:
BOOST_FOREACH(升压:: property_tree :: ptree中:: VALUE_TYPE&安培; V,pt.get_child(a.modules))
{
性病::法院LT&;&LT; v.second.get_child(&LT; xmlattr&GT; .TYPE)数据()&LT;&LT;的std :: ENDL;
性病::法院LT&;&LT; v.second.get_child(&LT; xmlattr&GT; .Reference点)。数据()&LT;&LT;的std :: ENDL;
}还有一个从这里服用。
//解析XML ...
BOOST_FOREACH(升压:: property_tree :: ptree中:: VALUE_TYPE&安培; V,pt.get_child(a.modules))
{
常量的boost :: property_tree :: ptree中和放大器;属性= v.second.get_child(&LT; xmlattr&gt;中,提振:: property_tree :: ptree中());
BOOST_FOREACH(常量的boost :: property_tree :: ptree中:: VALUE_TYPE&安培; V,属性)
{
性病::法院LT&;&LT; v.first.data()&所述;&下;的std :: ENDL;
性病::法院LT&;&LT; v.second.data()&所述;&下;的std :: ENDL;
}
}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
解决方案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; } }
这篇关于解析XML文件升压C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!