首页 > 其他分享 >POCO库-安装配置

POCO库-安装配置

时间:2022-11-26 20:37:34浏览次数:61  
标签:XML Poco doc 配置 POCO child include 安装

参考文献

(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;
}

标签:XML,Poco,doc,配置,POCO,child,include,安装
From: https://www.cnblogs.com/angelia-wang/p/16928228.html

相关文章

  • POCO_XML
    POCO简单写XML文档参考:https://blog.csdn.net/ma52103231/article/details/7609868先介绍一下XML文档中有哪些元素:Element-文档中某个节点Attr-文档中某个节点的属性......
  • Windows之应用安装程序 —— winget
    大家都用过Linux中的应用程序安装工具,如yum、apt、rpm等工具进行安装自己想要的一些工具或则软件之类的,当然Linux操作系统还是很强大的有很多类似的命令来安装我们所需要的......
  • 安装最新版git
    不能用yuminstallgit,因为版本太低,且无法控制==========正文开始:1、从git官网下载git压缩包https://github.com/git/git/#readme2、下载完成后是一个文件夹需要把......
  • PUTTY-0.75 下载安装及SSH远程连接方法
    (目录)Putty是一个免费的、Windows平台下的ssh远程登录工具。完全免费、小巧绿色、无需安装、操作简单,用它来远程管理Linux十分好用一、下载教程1.进入putty官网详见:h......
  • 【Autopsy数字取证篇】Autopsy数字取证软件的下载安装与优化配置
    【Autopsy数字取证篇】Autopsy数字取证软件的下载安装与优化配置Autopsy是一款免费开源的优秀数字取证(DigitalForensics)软件,提供与其他数字取证工具相同的核心功能,并提供......
  • 配置元数据
    配置元数据XML配置<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLS......
  • 阿里云服务器(Ubuntu)配置nextcloud个人网盘
    tags:UbuntuServerLinux写在前面最近迷恋上了云服务器的配置,感觉云服务器能做的事情太多了,不管是docker还是直接部署,都是相当方便快捷的,下面来看看在阿里云服务......
  • SpringCloud: polaris作为配置中心
    一、安装并启动polaris二、新建springcloud项目并加入相应依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xml......
  • CentOS7源码安装Nginx1.22
    CentOS7源码安装Nginx1.22一、安装下载源码包:wgethttp://nginx.org/download/nginx-1.22.1.tar.gz安装依赖:yum-yinstallgccmakepcrepcre-developensslope......
  • CentOS 7安装部署禅道
    1.查看Linux服务器版本信息#cat/etc/redhat-releaseCentOSLinuxrelease7.4.1708(Core) 2.禅道开源版安装包下载wget http://dl.cnezsoft.com/zentao/9.8.2/......