pugixml库之xml解析库

前言:

本文介绍c++编写的xml解析库——pugixml,能解析xml内容,支持xpath解析,同时能够跨linux平台,非常方便。

总结一下使用步骤和简单的使用方法:

使用pugixml库需要三个文件:pugiconfig.h/pugixml.h/pugixml.cpp,可直接从gugixml官网下载,将其加入工程,使用处如包含头文件pugiconfig.h/pugixml.h。

注意:如果想只是包含头文件pugixml.h,则需要在在pugiconfig.hpp中:
// Uncomment this to switch to header-only version
 #define PUGIXML_HEADER_ONLY
 #include "pugixml.cpp"
只需要将这两行的注释去掉即可。
  • 加载xml文件,使用xml_document类的load_file接口:
    std::strFile = "../test.xml";
    pugi::xml_document doc;
    if (!doc.load_file(strFile.c_str())) 
    {  return ;}
  • 加载xml格式的字符串,使用xml_document类的load接口:
  std::strText = "testing";
    pugi::xml_document doc;
    if (!doc.load(strText.c_str())) 
    {  return ;}

  • xml节点读取,如:xml文件params.xml:   

 <?xml version="1.0" encoding="utf-8" ?>  
   <root> 
    <!-- 输入参数配置 --> 
    <form ip="10.2.134.243" port="80" action="sisserver.php"> 
    <input name="data_type" value="POI" /> 
    <input name="query_type" value="TQUERY" /> 
    <input name="category" value="" /> 
 
    <!-- 查询词的返回结果xpath配置 --> 
    <xpath poiroot="//list/poi" idfield="pguid" namefield="name"/> 
    <!-- 评分权重配置 r1~r4-期望结果的权重,n1~n10-实际查询结果的排名权重--> 
    <weight> 
     <!-- 查询词正常得分阀值 --> 
     <threshold>3</threshold> 
     <!-- 计算分数分布情况的步长值 --> 
     <step>0.5</step> 
    </weight> 
  </root>

    读取代码:
std::string strFile = "/bak/workspace/test/src/params.xml";
    pugi::xml_document doc;
    if (!doc.load_file(strFile.c_str())) 
    {return 0;}
    pugi::xml_node form = doc.child("root").child("form");
    std::string ip = form.attribute("ip").value();
    std::string port = form.attribute("port").value();
   
    char cBuf[2083];
    sprintf(cBuf, "http://%s:%s/%s?", ip.c_str(), port.c_s());
    std::string strTemp(cBuf);
    std::string m_strURLBase = strTemp;

    for (pugi::xml_node input = form.first_child(); input;input = input.next_sibling())
    {
        std::string strValue = input.attribute("value").value();
        if (!strValue.empty()) 
        {
            std::string strName = input.attribute("name").value();
            sprintf(cBuf, "%s=%s&", strName.c_str(), strValue.c_str());
            std::string strTemp(cBuf);
            m_strURLBase += strTemp;
        }
    }
    
    //读取xpath
    pugi::xml_node xpath = doc.child("root").child("xpath");
    std::string m_strPOIRoot = xpath.attribute("poiroot").value();
    std::string m_strPOIID = xpath.attribute("idfield").value();

    //读取评分权重
    pugi::xml_node weight = doc.child("root").child("weight");
    float m_fThred = atof(weight.child_value("threshold"));
    float m_fStep = atof(weight.child_value("step"));

  • xpath解析,如xml格式的字符串strWebContent:
<?xml version="1.0" encoding="utf-8" ?>  
   <root> 
    <list count="3" time"10"> 
    <poi>
       <pguid>123</pguid>
       <name>xx1</name>
    </poi> 
    <poi>
       <pguid>456</pguid>
       <name>xx2</name>
    </poi> 
    <poi>
       <pguid>789</pguid>
       <name>xx3</name>
    </poi> 
    </list> 
  </root>

  其中,xpath根路径:m_strPOIRoot="//list/poi", 
  需要取值的项:strPOIID=“pguid”,strPOINam=“name”。

  读取代码:
 //从strWebContent内容中解析出pguid和name
  pugi::xml_document doc;
  pugi::xml_parse_result result = doc.load(strWebContent.c_str());
  if (!result)
  {return -1;}
  pugi::xpath_node_set tools = doc.select_nodes(m_strPOIRoot.c_str());
  for (pugi::xpath_node_set::const_iterator it = tools.begin(); 
      it !=  tools.end(); ++it)
  {
     pugi::xpath_node node = *it;
     string strPOI = node.node().child_value(m_strPOIID.c_str());
     string strName = node.node().child_value(m_strPOIName.c_str());
  }


写入xml方法:

先构造一个结构体

struct xml_string_writer : pugi::xml_writer
{
	std::string result;
	virtual void write(const void* data, size_t size)
	{
		result += std::string(static_cast<const char*>(data), size);
	}
};

void save_xml_config(const std::string &sql_ip, const std::string &sql_port, const std::string& user, const std::string& psw)
{
	pugi::xml_document doc;
	pugi::xml_node pre = doc.prepend_child(pugi::node_declaration);
	pre.append_attribute("version") = "1.0";
	pre.append_attribute("encoding") = "UTF-8";
	pre.append_attribute("standalone") = "no";

	pugi::xml_node node = doc.append_child("sys");
	pugi::xml_node child_node = node.append_child("item");
	child_node.append_attribute("name").set_value("mysqlIp");
	child_node.append_attribute("value").set_value(sql_ip.c_str());

	child_node = node.append_child("item");
	child_node.append_attribute("name").set_value("mysqlPort");
	child_node.append_attribute("value").set_value(sql_port.c_str());

	child_node = node.append_child("item");
	child_node.append_attribute("name").set_value("userName");
	child_node.append_attribute("value").set_value(user.c_str());

	child_node = node.append_child("item");
	child_node.append_attribute("name").set_value("userPassword");
	child_node.append_attribute("value").set_value(psw.c_str());
	std::string strXmlData;
	xml_string_writer writer;
	doc.save(writer);
	strXmlData = writer.result;
}
/************************************************************************/
/*
上述字符串内容显示结果
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <sys>
  <item name="mysqlIp" value="127.0.0.1" />
  <item name="mysqlPort" value="8888" />
  <item name="userName" value="admin" />
  <item name="userPassword" value="123465" />
  </sys>
*/
/************************************************************************/





  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PugiXML是一个轻量级的XML解析,适用于C++项目。安装PugiXML通常涉及以下几个步骤: 1. **下载源码**:首先从PugiXML的GitHub仓下载最新版本的源代码,地址是 https://github.com/peakhurst/pugixml/releases。 2. **获取编译文件**:如果你的项目使用的是CMake或者手动管理构建系统,你需要下载对应的编译文件(如`.zip`或`.tar.gz`),里面包含了预编译的静态和头文件。 3. **解压并移动到正确目录**:将下载的文件解压缩,并将其中的`include`和`lib`(或者其他指定的文件夹)移动到你的项目可找到的头文件和文件夹中。 4. **配置CMake** (如果使用CMake): - 在CMakeLists.txt文件中,添加`find_package(Pugixml REQUIRED)`来搜索。 - 然后添加`target_link_libraries(your_target pugixml::pugixml)`,确保你的目标链接了PugiXML。 5. **构建项目**:如果你使用的是命令行,运行`cmake .`(假设在源码根目录)然后`make`或`cmake --build .`。如果是IDE,按照IDE的指导配置项目设置。 6. **包含头文件**:在C++代码中,使用`#include <pugixml.hpp>`来引用PugiXML的头文件。 7. **链接**:确保编译器知道在哪里找到PugiXML的静态,这取决于你的构建环境。可能是通过命令行参数`-lpugixml`,或在IDE中配置链接器。 8. **测试安装**:编写一个小示例程序来验证PugiXML是否已经正确安装和链接,尝试解析或生成XML文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值