網(wǎng)站首頁(yè) 編程語言 正文
C++ 使用Poco庫(kù)實(shí)現(xiàn)XML的讀取和寫入
flyfish
文章目錄
- C++ 使用Poco庫(kù)實(shí)現(xiàn)XML的讀取和寫入
- read_xml函數(shù)實(shí)現(xiàn)讀取該xml文件
- write_xml函數(shù)實(shí)現(xiàn)xml文件的寫入
- XML的讀取和寫入整體代碼實(shí)現(xiàn)
- CMakeLists.txt文件配置
- 根據(jù)條件查找 XML 的值
假如example.xml內(nèi)容如下
<root_element>
<child_element_a a1="1" a2="2"/>
<child_element_b b1="3" b2="4"/>
root_element>
read_xml函數(shù)實(shí)現(xiàn)讀取該xml文件
輸出結(jié)果
node:0:root_element:
node:0:child_element_a:
map:a1:1
map:a2:2
node:0:child_element_b:
map:b1:3
map:b2:4
write_xml函數(shù)實(shí)現(xiàn)xml文件的寫入
寫入文件結(jié)果
<root_element>
<child_element_a a1="1" a2="2"/>
<child_element_b b1="3" b2="4"/>
<txt_element>txt_contenttxt_element>
root_element>
XML的讀取和寫入整體代碼實(shí)現(xiàn)
#include
#include
#include
#include
#include //Poco::AutoPtr
#include // Poco::XML::Document
#include // Poco::XML::Element
#include // Poco::XML::Text
#include // Poco::XML::CDATASection
#include // Poco::XML::ProcessingInstruction
#include // Poco::XML::Comment
#include // Poco::XML::DOMWriter
#include // Poco::XML::XMLWriter
#include
#include
#include
#include
#include
#include
#include
#include
#include
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
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;
}
CMakeLists.txt文件配置
cmake_minimum_required(VERSION 3.5)
project(example LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC 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.so"
"${POCO_LIB_DIR}/libPocoUtil.so"
"${POCO_LIB_DIR}/libPocoFoundation.so"
"${POCO_LIB_DIR}/libPocoNetSSL.so"
"${POCO_LIB_DIR}/libPocoXML.so")
add_executable(example
main.cpp
)
target_link_libraries(example "${POCO_LIBS}")
根據(jù)條件查找 XML 的值
假如這樣的XML文件
<root_node>
<child_node ID="0">
<person age="11" score="91"/>
child_node>
<child_node ID="1">
<person age="12" score="92"/>
child_node>
<child_node ID="2">
<person age="13" score="93"/>
child_node>
<child_node ID="3">
<person age="14" score="94"/>
child_node>
root_node>
代碼實(shí)現(xiàn)
int find_xml_value(Poco::AutoPtr<Poco::XML::Document> doc,int ID,std::string node,std::string attribute)
{
int ret=0;
Poco::XML::NodeIterator it(doc, Poco::XML::NodeFilter::SHOW_ALL);
std::string s="root_node/child_node["+std::to_string(ID)+"]/"+node;"root_node/child_node[ID]/person"
std::cout <<"1:"<<s<< std::endl;
Poco::XML::Node* a = it.nextNode();
Poco::XML::Node* root = a->getNodeByPath(s);
if(root)
{
std::cout <<"node:"<<":"<<root->nodeName()<<":"<< root->nodeValue()<< std::endl;
Poco::XML::NamedNodeMap* map = root->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;
if(n1 == attribute)
{
ret=std::stoi(v1);
break;
}
}
}
}
return ret;
}
int main(int argc, char *argv[])
{
Poco::XML::DOMParser parser;
Poco::AutoPtr<Poco::XML::Document> doc = parser.parse("./test.xml");
int ID=2;
std::string node="person";
std::string attribute="score";
int r= find_xml_value( doc, ID, node, attribute);
std::cout <<"result:"<<r<< std::endl;
return 0;
}
原文鏈接:https://blog.csdn.net/flyfish1986/article/details/124696363
相關(guān)推薦
- 2022-04-20 Python設(shè)計(jì)模式中的結(jié)構(gòu)型適配器模式_python
- 2022-06-28 python神經(jīng)網(wǎng)絡(luò)使用Keras構(gòu)建RNN訓(xùn)練_python
- 2022-03-25 C語言設(shè)計(jì)模式之命令模式介紹_C 語言
- 2022-03-19 Nginx純配置實(shí)現(xiàn)日志實(shí)時(shí)上報(bào)的思路與方法_nginx
- 2021-11-23 Linux系統(tǒng)下SystemC環(huán)境配置方法_Linux
- 2022-12-29 解決React報(bào)錯(cuò)Expected?`onClick`?listener?to?be?a?funct
- 2022-01-31 有關(guān)“數(shù)據(jù)統(tǒng)計(jì)”的一些概念 -- PV UV VV IP跳出率等
- 2022-10-09 C#實(shí)現(xiàn)折半查找算法_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支