如何使用 pugixml 读取节点?
How to read through nodes using pugixml?
我刚刚下载了 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>
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值?我愿意提供所有帮助。
快速入门页面中记录了几种方法:
- http://pugixml.org/docs/samples/traverse_iter.cpp
- http://pugixml.org/docs/samples/traverse_rangefor.cpp
- 有一个电力工作的树访客http://pugixml.org/docs/samples/traverse_walker.cpp
我可以推荐 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>
我刚刚下载了 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>
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值?我愿意提供所有帮助。
快速入门页面中记录了几种方法:
- http://pugixml.org/docs/samples/traverse_iter.cpp
- http://pugixml.org/docs/samples/traverse_rangefor.cpp
- 有一个电力工作的树访客http://pugixml.org/docs/samples/traverse_walker.cpp
我可以推荐 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>