Poco库XML文件的生成与解析
来源:互联网 发布:电脑有网络但上不到网 编辑:程序博客网 时间:2024/05/16 02:51
vs2010环境开发
注意:
编译时要记得添加静态库路径,以及包含的头文件路径。如果要直接跑程序,需要将涉及的静态库的动态库版本放置到程序所在的路径下。以便程序自己可以找到。
#include "Poco/DOM/Text.h" #include "Poco/DOM/Element.h" #include "Poco/DOM/Comment.h" #include "Poco/DOM/ProcessingInstruction.h" #include "Poco/DOM/Attr.h" #include "Poco/DOM/Document.h"#include "Poco/AutoPtr.h"#include "Poco/DOM/DOMWriter.h"#include "Poco/XML/XMLWriter.h"#include "Poco/FileStream.h"#include "Poco/Util/XMLConfiguration.h"
using Poco::Util::XMLConfiguration; using Poco::AutoPtr;#pragma comment(lib,"PocoFoundation.lib")#pragma comment(lib,"PocoXML.lib")#pragma comment(lib,"PocoUtil.lib")
xml文件的生成代码
AutoPtr<Poco::XML::Document> pDoc = new Poco::XML::Document; AutoPtr<Poco::XML::Element> myRoot = pDoc->createElement("Root"); AutoPtr<Poco::XML::Element> myChild = pDoc->createElement("Child"); AutoPtr<Poco::XML::Element> myGrandChild = pDoc->createElement("GrandChild"); AutoPtr<Poco::XML::Text> nameNode = pDoc->createTextNode("my_name_is_xiaoqiang"); AutoPtr<Poco::XML::ProcessingInstruction> pi = pDoc->createProcessingInstruction("xml","version='1.0' encoding='UTF-8'" ); AutoPtr<Poco::XML::Comment> comm = pDoc->createComment("new_day"); myGrandChild->appendChild(nameNode); myChild->appendChild(myGrandChild); myRoot->appendChild(myChild); pDoc->appendChild(pi); pDoc->appendChild(comm); pDoc->appendChild(myRoot); Poco::XML::DOMWriter write; write.setOptions(Poco::XML::XMLWriter::PRETTY_PRINT); Poco::FileStream ofs("./example.xml",std::ios::in); write.writeNode(ofs,pDoc);
xml文件的读取
要注意:读取文件时如果找不到对应的Key值,Poco内部会抛出异常直接崩溃。推荐捕获异常,然后返回false之类的才好。
AutoPtr<XMLConfiguration> pConfig(new XMLConfiguration("./example.xml")); //std::string prop1 = pConfig->getString("prop1"); //int prop2 = pConfig->getInt("prop2"); //std::string prop3 = pConfig->getString("prop3"); // //std::string prop4 = pConfig->getString("prop3.prop4"); //prop4 = pConfig->getString("prop3.prop4[@attr]"); //prop4 = pConfig->getString("prop3.prop4[1][@attr]"); std::string prop1 = pConfig->getString("Child.GrandChild"); int i=0; //int prop2 = pConfig->getInt("prop2"); //std::string prop3 = pConfig->getString("prop3"); // //std::string prop4 = pConfig->getString("prop3.prop4"); //prop4 = pConfig->getString("prop3.prop4[@attr]"); //prop4 = pConfig->getString("prop3.prop4[1][@attr]");
最好要捕获下异常,防止程序崩溃
std::string prop1;try{prop1 = pConfig->getString("Child.GrandChilds"); }catch(...){}
附录:
<config><prop1>value1</prop1><prop2>123</prop2><prop3><prop4 attr="value3" /><prop4 attr="value4" /></prop3></config>
<?xml version='1.0' encoding='UTF-8'?><!--new_day--><Root><Child><GrandChild>my_name_is_xiaoqiang</GrandChild></Child></Root>
1 0
- Poco库XML文件的生成与解析
- android xml文件的生成与解析
- Android中的XML解析与生成——Pull解析xml、实现xml文件的生成
- Java解析XML与生成XML文件
- XML的解析与生成
- xml的生成与解析
- XML的解析与生成
- XML的解析与生成
- XML的生成与解析
- XML的解析与生成
- pull解析与生成xml文件
- dom4j解析与生成xml文件
- xml解析与xml的生成
- Xml文件的解析与生成(pull解析和Dom4j解析)
- XML文件的解析和生成(DOM)
- XML文件的生成和解析
- xml文件的生成和解析
- Poco解析XML小例子
- perl模块安装
- 20. Valid Parentheses
- 巧用可变参数宏、编译器内置宏和printf输出调试信息
- 0.Intellij IDEA 添加jar包的三种方式
- windows下mysql忘记root密码的解决办法
- Poco库XML文件的生成与解析
- 循环双端队列示例C++_通话列表的保存
- Android 复杂的手势处理利用GestureOverView
- bsdiff 文档翻译
- 他贡献大于爱因斯坦,玩股票能赢过巴菲特
- JPA入门使用
- Android中三种超实用的滑屏方式汇总(ViewPager、ViewFlipper、ViewFlow)
- win7+eclipse+hadoop2.5.2开发mapreduce
- openstack neutron(tap、qvb、qvo详解)