C++中XML的读写操作(生成XML & 解析XML)

一、用Poco库

Poco库是下载、编译和使用:www.cnblogs.com/htj10/p/11380144.html

DOM(The Document Object Model)方式:

1. 生成XML

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

int main(int argc, char** argv)
{
    //Poco生成XML
    Poco::AutoPtr<Poco::XML::Document> pDoc = new Poco::XML::Document;
    Poco::AutoPtr<Poco::XML::ProcessingInstruction> pi = pDoc->createProcessingInstruction("xml","version='1.0' encoding='UTF-8'");
    Poco::AutoPtr<Poco::XML::Comment> pComment = pDoc->createComment("The information of some Universities.");
    Poco::AutoPtr<Poco::XML::Element> pRoot = pDoc->createElement("University_info");

    Poco::AutoPtr<Poco::XML::Element> pChild = pDoc->createElement("University");
    pChild->setAttribute("name", "Harvard");
    Poco::AutoPtr<Poco::XML::Element> pGrandchild1 = pDoc->createElement("school");
    pGrandchild1->setAttribute("name", "Secient");
    Poco::AutoPtr<Poco::XML::Element> pGrandchild2 = pDoc->createElement("school");
    pGrandchild2->setAttribute("name", "Mathematics");

    Poco::AutoPtr<Poco::XML::Element> pNumOfPeople = pDoc->createElement("people_counting");
    Poco::AutoPtr<Poco::XML::Text> pText = pDoc->createTextNode("123");
    pNumOfPeople->appendChild(pText);
    Poco::AutoPtr<Poco::XML::CDATASection> pCDATA = pDoc->createCDATASection("sql=select * from table1 where id<5");

    pDoc->appendChild(pi);
    pDoc->appendChild(pComment);
    pDoc->appendChild(pRoot);
    pRoot->appendChild(pChild);
    pChild->appendChild(pGrandchild1);
    pChild->appendChild(pGrandchild2);
    pGrandchild1->appendChild(pNumOfPeople);
    pRoot->appendChild(pCDATA);

    Poco::XML::DOMWriter writer;
    writer.setOptions(Poco::XML::XMLWriter::PRETTY_PRINT);// PRETTY_PRINT = 4
    writer.writeNode("./example.xml", pDoc);//直接写进文件
    //或者直接写进string
    std::stringstream sstr;
    writer.writeNode(sstr, pDoc);
    std::string s = sstr.str();

    return 0;
}

 

 2. 解析xml

#include <Poco/AutoPtr.h>            //fro Poco::AutoPtr
#include <Poco/DOM/Document.h>        //for Poco::XML::Document
#include <Poco/DOM/DOMParser.h>        //for Poco::XML::DOMParser
#include <Poco/DOM/NodeIterator.h>    //for Poco::XML::NodeIterator
#include <Poco/DOM/NodeFilter.h>    //for Poco::XML::NodeFilter
#include <Poco/DOM/Node.h>            //for Poco::XML::Node
#include <Poco/DOM/NamedNodeMap.h>    //for Poco::XML::NamedNodeMap
int main(int argc, char** argv)
{
    //解析xml
    Poco::XML::DOMParser parser;
    //parser.setFeature(Poco::XML::DOMParser::FEATURE_FILTER_WHITESPACE, true);//过滤空白符
    Poco::AutoPtr<Poco::XML::Document> pDoc = parser.parse("./example.xml");//解析xml文件
    //Poco::AutoPtr<Poco::XML::Document> pDoc = parser.parseString(strXml);//解析xml字符串
    Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ALL);//可以过滤 SHOW_ELEMENT SHOW_ATTRIBUTE  SHOW_TEXT  SHOW_CDATA_SECTION 等
    Poco::XML::Node* pNode = it.nextNode();
    while (pNode)
    {
        //if (pNode->nodeType() != Poco::XML::Node::ELEMENT_NODE)//过滤 非element
        //    {pNode = it.nextNode(); continue;}
        std::string sName = pNode->nodeName();
        std::string sValue = pNode->nodeValue();
        std::string sText = pNode->innerText();
        Poco::XML::NamedNodeMap* map = pNode->attributes();
        if (map)
        {
            for (int i = 0; i < map->length(); ++i)
            {
                Poco::XML::Node* attr = map->item(i);
                std::string sAttrName = attr->nodeName();
                std::string sAttrValue = attr->nodeValue();
                //...
            }
        }
        pNode = it.nextNode();
    }
    return 0;
}
节点 nodeName nodeValue
Document "#document" ""
Comment "#comment" ""
Element tag名 ""
Text "#text" 文本字符串
CDATASection "#cdata-section" CDATA内容
     

 

 C++中XML的读写操作(生成XML & 解析XML)

任何元素都被抽象成Node,同时又分为三种类型的节点。(Attr和Notation看成一种)
第一种类型:CharacterData,这类Node是Name不可变,而Value可以由用户自定义。
第二种类型:AbstractContainerNode,这类Node有个特点,即含有属性,特别的对于Element节点,Name可以由用户自定义,而Value不可变。
第三种类型:右边两个,它们既可以改变Name,也可以改变Value。
————————————————
原文链接:https://blog.****.net/ma52103231/article/details/7701880

 

SAM(The Simple API for XML)方式:

C++中XML的读写操作(生成XML & 解析XML)C++中XML的读写操作(生成XML & 解析XML)

 

 

 参考官方文档:https://pocoproject.org/slides/170-XML.pdf