c++ - 如何在访问每个节点的同时解析 CPP 中的 XML 结构数据?
问题描述
我对 C++ 中的 XML 解析非常陌生。我想解析 XML。所以,我正在使用 PugiXML 库。我主要想从每个子节点获取值。
这是我写到这里的示例代码,然后我不明白下一步该做什么。
void XML()
{
//Suppose I'm getting XML data in the form of a string, and store in string variable **xmlString**
//Using 3rd party lib Pugixml parser want to store all the **xmlString data in doc**.
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(xmlString.c_str());
// I want value will store the data "Sheet1" as per the syntax. But somehow, doc store the value till here **<?xml version = "1.0" encoding = "UTF-8"?><Graphics**
string value = doc.child("Graphics").child("SheetInfo").attribute("SheetName").value();
}
这是存储在xmlString中的示例 XML 数据。
<?xml version = "1.0" encoding = "UTF-8"?>
<Graphics>
<SheetInfo>
<SheetName>Sheet1</SheetName>
<Circle>
<IGDSElement>
<Type>89</Type>
</IGDSElement>
<cPt_x>0.212050</cPt_x>
<cPt_y>0.148307</cPt_y>
<orientation>0</orientation>
</Circle>
<SheetInfo>
<Graphics>
请帮助我在前往每个节点时解析此 XML 格式数据。
解决方案
如果要访问每个节点,可以for
循环遍历:
for(const auto& child : doc.child("Graphics"))
cout << child.child_value() << endl;
要查找特定节点,您可以使用XPath
. 以下XML
是解析通常需要的各种示例。
推荐阅读
- django - Django REST Serializer 使用错误的模型进行序列化
- python - python文件写入程序运行时如何更新桌面上的文件大小
- javascript - 使用 d3.js 更新表数据
- c# - C#捕获从不在进程中的函数返回的异常?
- r - 如何设置仅在输入 3 时才显示集合向量的函数?
- javascript - 如果 URI 没有改变,例如在单页应用程序上,如何检测用户是否在新页面上?
- angular - Angular Kendo UI 全局访问
- php - 内连接循环通过
- git - 如何 git rebase 从另一个分支直接到 master 分支?
- javascript - 受控数字比例映射