gpt4 book ai didi

c++ - 如何使用 pugixml 读取节点?

转载 作者:行者123 更新时间:2023-11-30 02:38:40 39 4
gpt4 key购买 nike

我刚刚下载了 pugixml 库,我正在努力使其适应我的需要。它主要面向我未使用的 DOM 样式。我存储的数据如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<profile>
<points>
<point>
<index>0</index>
<x>0</x>
<y>50</y>
</point>
<point>
<index>1</index>
<x>2</x>
<y>49.9583</y>
</point>
<point>
<index>2</index>
<x>12</x>
<y>50.3083</y>
</point>
</points>
</profile>

Pugixml guide说:

It is common to store data as text contents of some node - i.e. This is a node. In this case, node does not have a value, but instead has a child of type node_pcdata with value "This is a node". pugixml provides child_value() and text() helper functions to parse such data.

但我在使用这些方法时遇到问题,我没有得到节点值。

#include "pugixml.hpp"

#include <string.h>
#include <iostream>

int main()
{
pugi::xml_document doc;
if (!doc.load_file("/home/lukasz/Programy/eclipse_linux_projects/xmlTest/Debug/pidtest.xml"))
return -1;

pugi::xml_node points = doc.child("profile").child("points");

for (pugi::xml_node point = points.first_child(); point; point = points.next_sibling())
{
// ?
}


return 0;
}

如何读出for里面的index,x,y值?我愿意提供所有帮助。

最佳答案

有几种方法,在快速入门页面中有介绍:

我可以推荐 Xpath 吗?

#include <pugixml.hpp>
#include <iostream>

int main()
{
pugi::xml_document doc;

if (doc.load_file("input.txt")) {
for (auto point : doc.select_nodes("//profile/points/point")) {
point.node().print(std::cout, "", pugi::format_raw);
std::cout << "\n";
}
}
}

打印

<point><index>0</index><x>0</x><y>50</y></point>
<point><index>1</index><x>2</x><y>49.9583</y></point>
<point><index>2</index><x>12</x><y>50.3083</y></point>

关于c++ - 如何使用 pugixml 读取节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30593324/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com