CentOS 7 使用pugixml 库

安装

pugixml Git下载地址:https://github.com/zeux/pugixml

步骤1:首先,你需要下载pugixml 的源代码。你可以从Github或者源代码官方网站下载。并上传至/usr/local/source_code/

步骤2:下载完成后,需要将源代码解压,可以使用以下命令:

复制代码
 tar -zxvf pugixml-1.13.tar.gz

步骤3:解压后,切换到源代码目录:

复制代码
[root@localhost source_code]# cd pugixml-1.13

步骤4:生成pugixml 静态库,执行如下指令:

复制代码
mkdir build
cd build
cmake ..
make && make install

[root@localhost build]# cd ..
[root@localhost pugixml-1.13]# cmake .
-- The CXX compiler identification is GNU 8.3.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /opt/rh/devtoolset-8/root/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/source_code/pugixml-1.13
[root@localhost pugixml-1.13]# make && make install
[ 50%] Building CXX object CMakeFiles/pugixml-static.dir/src/pugixml.cpp.o
[100%] Linking CXX static library libpugixml.a
[100%] Built target pugixml-static
Consolidate compiler generated dependencies of target pugixml-static
[100%] Built target pugixml-static
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/lib64/libpugixml.a
-- Installing: /usr/local/lib64/cmake/pugixml/pugixml-targets.cmake
-- Installing: /usr/local/lib64/cmake/pugixml/pugixml-targets-noconfig.cmake
-- Installing: /usr/local/lib64/cmake/pugixml/pugixml-config-version.cmake
-- Installing: /usr/local/lib64/cmake/pugixml/pugixml-config.cmake
-- Installing: /usr/local/lib64/pkgconfig/pugixml.pc
-- Up-to-date: /usr/local/include/pugiconfig.hpp
-- Up-to-date: /usr/local/include/pugixml.hpp

温馨提示:完成以上步骤后,在/usr/local/lib下会生成libpugixml.so动态库和/usr/local/include下生成pugixml.hpp头文件。你可以使用以下命令查看是否安装成功:

复制代码
ls /usr/local/lib | grep pugixml
ls /usr/local/include | grep pugixml

如果看到,则说明成功安装了pugixml库 .

快速入门

在/usr/local/source_code 新增pugixml_demo 目录并新增test_pugixml.cpp 文件,文件内容如下:

复制代码
#include "pugixml.hpp"
#include <iostream>
using namespace std;

int main(){
    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_file("input.xml");
    if(!result)
        return -1;
    cout << "parse result: " << endl;
    pugi::xml_node root_node = doc.child("platform");
    pugi::xml_node ip_node = root_node.child("ip");
    pugi::xml_node port_node = root_node.child("port");
    pugi::xml_node key_node = root_node.child("key");
    cout << ip_node.text().as_string() << endl;
    cout << port_node.text().as_int() << endl;
    cout << key_node.text().as_string() << endl;
 return 0;
}

在test_pugixml.cpp 文件所在目录新增input.xml 文件,文件内容如下:

复制代码
<platform>
    <ip>192.168.1.2</ip>
    <port>50000</port>
    <key>keyvalue</key>
</platform>

编译源码并执行:

复制代码
[root@localhost pugixml_demo]# g++ test_pugixml.cpp -o test_pugixml -std=c++11 -lpugixml
[root@localhost pugixml_demo]# ll
总用量 340
-rw-r--r--. 1 root root     95 11月 22 18:07 input.xml
-rwxr-xr-x. 1 root root 338744 11月 22 19:20 test_pugixml
-rw-r--r--. 1 root root    615 11月 22 19:20 test_pugixml.cpp
[root@localhost pugixml_demo]# ./test_pugixml
parse result:
192.168.1.2
50000
keyvalue

入门详细

请参考:pugi 快速入门

pugixml Git地址

pugixml 官网开发指南

相关推荐
fiveym7 分钟前
持续交付与持续部署(CD)深度解析:定义差异、流程架构与交付模式对比
运维·ci/cd·架构
PascalMing9 分钟前
ubuntu 24.04安装dotnet 10日志
linux·运维·ubuntu·dotnet10
oMcLin12 分钟前
如何在 CentOS 8 上部署并优化 Jenkins 2.x 流水线,提升 CI/CD 流程的自动化与高效性
ci/cd·centos·jenkins
optimistic_chen15 分钟前
【Docker入门】容器技术
linux·运维·服务器·docker·容器
Lueeee.16 分钟前
2.智梯云枢・全维管控广告系统——解决串口卡顿 + 优化稳定性
linux·运维·服务器
南行*17 分钟前
C语言Linux环境编程
linux·c语言·开发语言·网络安全
海清河晏11117 分钟前
Linux进阶篇:HTTP协议
linux·运维·http
June`19 分钟前
IO模型全解析:从阻塞到异步(高并发的reactor模型)
linux·服务器·网络·c++
oMcLin24 分钟前
如何在 CentOS 7.9 上配置并调优 Docker Swarm 集群,确保跨多个节点的高效服务发现与负载均衡?
docker·centos·服务发现
ASS-ASH25 分钟前
快速处理虚拟机磁盘扩容问题
linux·数据库·vmware·虚拟机·磁盘扩容