参考文献

(17条消息) C++ 使用Poco库实现XML的读取和写入_西西弗Sisyphus的博客-CSDN博客_c++写入xml

全局安装

pocoproject/poco: The POCO C++ Libraries are powerful cross-platform C++ libraries for building network- and internet-based applications that run on desktop, server, mobile, IoT, and embedded systems.

安装到系统路径: The default install location is /usr/local/ on Linux and macOS and C:\Program Files (x64)\ on Windows and can be overridden by setting the CMAKE_INSTALL_PREFIX CMake variable.

$ git clone -b master https://github.com/pocoproject/poco.git
$ cd poco
$ mkdir cmake-build
$ cd cmake-build
$ cmake ..
$ cmake --build . --config Release

sudo cmake --build . --target install    // 安装
sudo cmake --build . --target uninstall  // 卸载

CMakeLists.txt文件配置实例

cmake_minimum_required(VERSION 3.22)
project(poco_xml_to_kv)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# set the POCO paths and libs
set(POCO_PREFIX "/usr/local") # the directory containing "include" and "lib"
set(POCO_INCLUDE_DIR "${POCO_PREFIX}/include")
set(POCO_LIB_DIR "${POCO_PREFIX}/lib")
set(POCO_LIBS
        "${POCO_LIB_DIR}/libPocoNet.dylib"
        "${POCO_LIB_DIR}/libPocoUtil.dylib"
        "${POCO_LIB_DIR}/libPocoFoundation.dylib"
#        "${POCO_LIB_DIR}/libPocoNetSSL.dylib"
        "${POCO_LIB_DIR}/libPocoXML.dylib")

link_directories(${POCO_LIB_DIR}) # 添加非标准的共享库搜索路径

add_executable(${PROJECT_NAME}
        main.cpp
        )

include_directories(${POCO_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${POCO_LIBS}) # 把目标文件与库文件进行链接

xml实例

#include <string>
#include <streambuf>
#include <sstream>
#include <iostream>
#include <Poco/AutoPtr.h> //Poco::AutoPtr
#include <Poco/DOM/Document.h> // Poco::XML::Document
#include <Poco/DOM/Element.h>  // Poco::XML::Element
#include <Poco/DOM/Text.h>       // Poco::XML::Text
#include <Poco/DOM/CDATASection.h>    // Poco::XML::CDATASection
#include <Poco/DOM/ProcessingInstruction.h> // Poco::XML::ProcessingInstruction
#include <Poco/DOM/Comment.h>  // Poco::XML::Comment
#include <Poco/DOM/DOMWriter.h> // Poco::XML::DOMWriter
#include <Poco/XML/XMLWriter.h> // Poco::XML::XMLWriter

#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/Node.h>
#include <Poco/DOM/NamedNodeMap.h>
#include <Poco/XML/XMLString.h>
#include <Poco/XML/XMLException.h>
#include <Poco/XML/XMLStream.h>
#include <Poco/DOM/NodeIterator.h>
#include <Poco/DOM/NodeFilter.h>

void read_xml()
{
 
    Poco::XML::DOMParser parser;

    Poco::AutoPtr<Poco::XML::Document> doc = parser.parse("./example.xml");
    Poco::XML::NodeIterator it(doc, Poco::XML::NodeFilter::SHOW_ALL);//SHOW_ELEMENT SHOW_ATTRIBUTE  SHOW_TEXT  SHOW_CDATA_SECTION
    Poco::XML::Node* node = it.nextNode();

    int i=0;
    while (node)
    {
        if (node->nodeType() != Poco::XML::Node::ELEMENT_NODE)//code example
        {
            node = it.nextNode();
            continue;
        }
        if(node->nodeName() == "#text") //code example
        {
            node = it.nextNode();
            continue;
        }
        if(node->nodeName() == "#cdata-section")//code example
        {
            node = it.nextNode();
            continue;
        }

        std::cout <<"node:"<<i<<":"<<node->nodeName()<<":"<< node->nodeValue()<< std::endl;
        Poco::XML::NamedNodeMap* map = node->attributes();
        if (map)
        {
            for (size_t i = 0; i < map->length(); ++i)
            {
                Poco::XML::Node* c = map->item(i);
                std::string n1 = c->nodeName();
                std::string v1 = c->nodeValue();

                std::cout <<"map:"<<n1<<":"<<v1<< std::endl;
            }
        }
        node = it.nextNode();
    }

}

void write_xml()
{
    Poco::AutoPtr<Poco::XML::Document> doc = new Poco::XML::Document;
    //custom declaration
    Poco::AutoPtr<Poco::XML::ProcessingInstruction> pi = doc->createProcessingInstruction("xml","version='1.0' encoding='UTF-8'");
    Poco::AutoPtr<Poco::XML::Comment> comment = doc->createComment("This is comment.");
    Poco::AutoPtr<Poco::XML::Element> e_root = doc->createElement("root_element");

    Poco::AutoPtr<Poco::XML::Element> e_child_a = doc->createElement("child_element_a");
    e_child_a->setAttribute("a1", "1");
    e_child_a->setAttribute("a2", "2");

    Poco::AutoPtr<Poco::XML::Element> e_child_b = doc->createElement("child_element_b");
    e_child_b->setAttribute("b1", "3");
    e_child_b->setAttribute("b2", "4");



    Poco::AutoPtr<Poco::XML::Text> txt = doc->createTextNode("txt_content");
    Poco::AutoPtr<Poco::XML::CDATASection> cdata = doc->createCDATASection("ignore parse txt !@#$%^&*()");

    doc->appendChild(pi);
    doc->appendChild(comment);
    doc->appendChild(e_root);
    e_root->appendChild(e_child_a);
    e_root->appendChild(e_child_b);

    e_root->appendChild(cdata);
    e_root->appendChild(txt);

    Poco::XML::DOMWriter writer;

    //writer.setOptions(Poco::XML::XMLWriter::CANONICAL);
    //writer.setOptions(Poco::XML::XMLWriter::PRETTY_PRINT_ATTRIBUTES); //
    //writer.setOptions(Poco::XML::XMLWriter::CANONICAL_XML);
    //writer.setOptions(Poco::XML::XMLWriter::WRITE_XML_DECLARATION);// add <?xml version='1.0' encoding='UTF-8'?>
    writer.setOptions(Poco::XML::XMLWriter::PRETTY_PRINT);

    writer.writeNode("./example.xml", doc);
    //string test
    std::stringstream sstr;
    writer.writeNode(sstr, doc);
    std::string s = sstr.str();
    std::cout <<s<< std::endl;
}

int main(int argc, char *argv[])
{
    write_xml();
    //read_xml();
    return 0;
}

原文地址:http://www.cnblogs.com/angelia-wang/p/16928228.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性