程序设计:pugixml

Pugixml

一个方便小巧高效的xml读写库,可以纯头文件运行,包含pugixml.hpp即可使用

#include "pugixml.hpp"
 
#include <string.h>
#include <iostream>
 
int main()
{
    pugi::xml_document doc;
    if (!doc.load_file("xgconsole.xml")) return -1;
}

pugixml 中使用函数select_nodes获取节点,可以使用xpath搜索节点

获得节点后可以使用node获取xpath查找到的节点,然后可以通过attribute获取相关属性

#include "pugixml.hpp"
 
#include <string.h>
#include <iostream>
 
int main()
{
    pugi::xml_document doc;
    if (!doc.load_file("xgconsole.xml")) return -1;
    //按xpath查找节点
    pugi::xpath_node_set xNodeSet = doc.select_nodes("//ValidRange/GeoPoint");
    if (xNodeSet.empty()||xNodeSet.size()<=0)
    {
        return -1;
    }
    //读取属性信息
    std::vector<double> rangexy;
    for (pugi::xpath_node_set::iterator it = xNodeSet.begin(); it != xNodeSet.end(); ++it)
    {
        pugi::xml_node geoPoint = it->node();
        rangexy.push_back(geoPoint.attribute("X").as_double());
        rangexy.push_back(geoPoint.attribute("Y").as_double());
    }
    return 0;
}
#include "pugixml.hpp"
 
#include <iostream>
 
int main()
{
    // 从string中创建doc
 
    pugi::xml_document doc;
    pugi::xml_node dec = doc.prepend_child(pugi::node_declaration);
    dec.append_attribute("version").set_value("1.0");
    dec.append_attribute("encoding").set_value("utf-8");
    pugi::xml_node product = doc.append_child("Product");
    pugi::xml_node path = product.append_child("path");
    path.append_child(pugi::node_pcdata).set_value("temp.gltf");
    pugi::xml_node Projection = doc.append_child("Projection");
    pugi::xml_attribute type = Projection.append_attribute("type");
    type.set_value("wkt");
    pugi::xml_node Mapping = Projection.append_child("Mapping");
    Mapping.append_child(pugi::node_pcdata).set_value("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
    doc.save_file("temp.xml");
}
  • 程序设计/pugixml.txt
  • 最后更改: 2022/09/28 14:46
  • 60.188.10.145