程序设计:pugixml

Pugixml

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

更多使用请参考 pugixml文档

可以用load_file或者load_string读取xml文件或者字符串

#include "pugixml.hpp"
 
#include <string.h>
#include <iostream>
 
int main()
{
    pugi::xml_parse_result result;
    pugi::xml_document doc;
    result = doc.load_file("xgconsole.xml");
 
    if (result)
    {
        //成功
        std::cout << "XML [" << source << "] parsed without errors, attr value: [" << doc.child("node").attribute("attr").value() << "]\n\n";
    }
    else
    {
        //失败
        std::cout << "XML [" << source << "] parsed with errors, attr value: [" << doc.child("node").attribute("attr").value() << "]\n";
        std::cout << "Error description: " << result.description() << "\n";
        std::cout << "Error offset: " << result.offset << " (error at [..." << (source + result.offset) << "]\n\n";
    }
}

pugi::xml_node类中可以调用child或者next_sibling来实现遍历节点

#include "pugixml.hpp"
 
#include <string.h>
#include <iostream>
 
int main()
{
    pugi::xml_document doc;
    if (!doc.load_file("xgconsole.xml")) return -1;
 
    pugi::xml_node tools = doc.child("Profile").child("Tools");
 
    // 遍历属性
    for (pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling())
    {
        std::cout << "Tool:";
 
        for (pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute())
        {
            std::cout << " " << attr.name() << "=" << attr.value();
        }
 
        std::cout << std::endl;
    }
 
    std::cout << std::endl;
 
    // tag::data[]
    for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
    {
        std::cout << "Tool " << tool.attribute("Filename").value();
        std::cout << ": AllowRemote " << tool.attribute("AllowRemote").as_bool();
        std::cout << ", Timeout " << tool.attribute("Timeout").as_int();
        std::cout << ", Description '" << tool.child_value("Description") << "'\n";
    }
 
    std::cout << std::endl;
 
    // tag::contents[]
    std::cout << "Tool for *.dae generation: " << tools.find_child_by_attribute("Tool", "OutputFileMasks", "*.dae").attribute("Filename").value() << "\n";
 
    for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
    {
        std::cout << "Tool " << tool.attribute("Filename").value() << "\n";
    }
}

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 xPathNode = doc.select_node("//ValidRange/GeoPoint");
    if (!xPathNode )
    {
        return -1;
    }
    pugi::xml_node ndoe = xPathNode.node();
    //TODO获取属性或者做其他处理...
 
    //按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
  • 最后更改: 2021/09/20 04:42
  • 106.11.157.47