在c++中解析XML元素
Parse XML elements in C++
我使用的是tinyxml2,我想在c++中解析XML中的一些元素。例如
<root>
<First x="1" y="2">
<Second x = "1">
<Second y = "2">
</root>
我只能解析"Second"元素中的x。
#include <stdio.h>
#include "tinyxml2.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace tinyxml2;
using namespace std;
int main(){
tinyxml2::XMLError eResult = xml_doc.LoadFile("test.xml");
if (eResult != tinyxml2::XML_SUCCESS) return false;
tinyxml2::XMLNode* root = xml_doc.FirstChildElement("root");
if (root == nullptr) return false;
tinyxml2::XMLElement* First = root->FirstChildElement("First");
if (First == nullptr) return false;
double x1 = std::stod(First->Attribute("x"));
double y1 = std::stod(First->Attribute("y"));
tinyxml2::XMLElement* Second = root->FirstChildElement("Second");
if (Second == nullptr) return false;
double x2 = std::stod(Second->Attribute("x"));
double y2 = std::stod(Second->Attribute("y"));
system("pause");
}
当我为"第一个"元素或"第二个y"尝试相同的方法时,它只是向我显示错误。我该怎么办?
您定义了两次"double x"。试着
double first_x = std::stod(First->Attribute("x"));
double first_y = std::stod(First->Attribute("y"));
和
double second_x = std::stod(Second->Attribute("x"));
你的编译器应该已经阻止你了,但是一定要注意警告!
相关文章:
- 使用 libxml2 解析 XML 文件时缺少元素值
- 使用提升属性树检查 xml 文件中是否存在元素
- 使用 qt c++ 检查 Xml 中是否存在根元素
- 使用 TinyXML 解析 XML 元素 - 无限循环
- 如何使用libxml2从C 中的XML文件中检索节点和特定元素字符串而不使用XPATH
- 如何使用XPath使用libxml2从C 中的XML文件中检索节点和特定元素字符串
- 正在更新微小Xml元素中的数据
- 忽略GSOAP中的未知XML元素节点
- 将xml字符串嵌套到xml元素中的语法
- PugiConvert xml元素内容到C++数组
- 有人可以解释为什么我的XML解析器跳过这些HTML元素的原因
- 如何使用 qt 删除嵌套的 xml 元素
- CodeSynthesis XSD CXX 不解析 XML 文件,表示未声明属性/元素
- 使用 cpp 在 opencv 中的 xml 文件中追加元素
- Qt XML读取元素,并且只获取根
- Qt更新单个XML元素
- 忽略xml元素中的字符
- 使用TinyXML解析XML元素
- 使用TinyXML解析XML元素
- 在c++中解析XML元素