使用boost c++解析XML
Parsing XML using boost c++
我有一些XML,如下所示:
<animal>
<name>shark</name>
<color>blue</color>
</animal>
<animal>
<name>dog</name>
<color>black</color>
</animal>
我试着只打印动物的名字(鲨鱼和狗)。我正在使用boost,所以我尝试了以下代码:
ptree pt;
boost::property_tree::read_xml("animals.xml", pt);
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("animal"))
{
std::cout << v.second.data() << std::endl;
}
但它只打印鲨鱼和蓝色。我不确定问题出在哪里,也找不到好的例子。有人能提供一些建议吗?
xml必须只有一个根对象。。。你有2个……试试这样的东西:
<animals>
<animal>
<name>shark</name>
<color>blue</color>
</animal>
<animal>
<name>dog</name>
<color>black</color>
</animal>
</animals>
令我惊讶的是,PugiXML开箱即用地支持它:
#include <iostream>
#include <fstream>
#include <pugixml.hpp>
int main() {
pugi::xml_document doc;
std::ifstream ifs("input.txt", std::ios::binary);
pugi::xml_parse_result r = doc.load(ifs, pugi::parse_default);
std::cout << "PARSED " << r.status << " (" << r.description() << "):n";
doc.save(std::cout, " ", pugi::format_default);
std::cout << "DONEn";
}
打印
PARSED 0 (No error):
<?xml version="1.0"?>
<animal>
<name>shark</name>
<color>blue</color>
</animal>
<animal>
<name>dog</name>
<color>black</color>
</animal>
DONE
相关文章:
- 防止在C++中选择错误文件时提升 xml 解析器崩溃
- 在 c++ 中将简单的 xml 解析为字符串
- 如何使用 Boost XML 解析器
- Boost::p roperty_tree:在XML解析器中使用std::vector<>在一个键中存储多个值
- 将Harrcascade加载到CascadeClalsifier时,XML解析错误
- Expat XML解析器(静态链接,DLL错误(缺少Define?)
- 我正在尝试使用C++开发XML解析器.但是在返回时声明的枚举值上遇到错误
- 与boost::property_tree XML解析器一起使用时,boost::协同程序库崩溃
- 带有 HTML 报告生成器的 c++ XML 解析器的体系结构
- 操作系统便携式 C/C++ XML 解析器
- 在 XML 解析程序上没有可行的重载
- 快速 XML 解析不当行为
- 从XML解析字符串时添加到n的反斜杠
- 支持SOAP请求/响应的最佳XML解析器
- XML 解析器包装器
- 使用 QT 进行 XML 解析
- c ++如何使用boost xml解析器读取XML并存储在map中
- 有人可以解释为什么我的XML解析器跳过这些HTML元素的原因
- 如何在boost中解析属性?Soap xml解析
- BoostSpirit,如何扩展xml解析