xml解析库xerces学习笔记一

#include <iostream>
using namespace std;


#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include <xercesc/dom/DOM.hpp>


#pragma comment(lib,"xerces-c_3.lib")  
using namespace xercesc;


int main()
{
    // 一、xml文档解析之“建立本地XML文件输入数据的方法”
    // 初始化XML类
    XMLPlatformUtils::Initialize();


    // 获取DOM实例
    XMLCh tempStr[100];
    XMLString::transcode("Range", tempStr, 99);
    DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr);


    //生成本地文件,已存在的会被清空,不存在的会自动创建
    XMLFormatTarget* Targe = NULL;
    Targe = new LocalFileFormatTarget( "testXML.xml" );


    //输出流指向文件
    DOMLSOutput* Output = impl->createLSOutput();
    Output->setByteStream( Targe );


    //生成序列器
    DOMLSSerializer * Serializer = impl->createLSSerializer();


    //创建xml文档
    XMLString::transcode("root", tempStr, 99);
    DOMDocument*   doc = impl->createDocument(0, tempStr, 0);


    //获取xml文档根节点
    DOMElement*   root = doc->getDocumentElement();


    // 创建文档节点
    XMLString::transcode("FirstElement", tempStr, 99);
    DOMElement*   e1 = doc->createElement(tempStr);


    //追加到根节点下面
    root->appendChild(e1);


    XMLString::transcode("SecondElement", tempStr, 99);
    DOMElement*   e2 = doc->createElement(tempStr);


    XMLCh tempStr1[100];
    XMLString::transcode("Name", tempStr1, 99);
    XMLCh tempStr2[100];
    XMLString::transcode("Value", tempStr2, 99);
    e2->setAttribute(tempStr1, tempStr2);
    //追加到根节点下面,和e1同级
    root->appendChild(e2);


    /*


    XMLString::transcode("ThreeElement", tempStr, 99);
    DOMElement*   e3 = doc->createElement(tempStr);


    XMLCh tempStr3[100];
    XMLString::transcode("SecondElement", tempStr2, 99);


    root->insertBefore(e3, e1);
    XMLString::transcode("aTextNode", tempStr, 99);
    DOMText* textNode = doc->createTextNode(tempStr);
    e1->appendChild(textNode);


    cout << XMLString::transcode(textNode->getNodeValue()) << endl;
    cout << XMLString::transcode(e2->getTagName()) << endl;


    //optionally, call release() to release the resource associated with the range after done
    DOMRange* range = doc->createRange();
    range->release();


    //removedElement is an orphaned node, optionally call release() to release associated resource
    DOMNode* removedElement = root->removeChild(e2);
    removedElement->release();
    
    XMLString::transcode("*", tempStr, 99);
    DOMNodeList*    nodeList = doc->getElementsByTagName(e1->getTagName());


    if (NULL != nodeList)
    {
        XMLSize_t size = nodeList->getLength();
        XMLSize_t szBegin = 0;
        for (; szBegin < size; ++szBegin)
        {
            DOMNode* pDOMNode = nodeList->item(szBegin);
            if (NULL != pDOMNode)
            {
                cout << "pDOMNode->getNodeName() = " << XMLString::transcode(pDOMNode->getNodeName()) << endl;
                if (NULL != pDOMNode->getNodeValue())
                {
                    cout << "pDOMNode->getNodeValue() = " << XMLString::transcode(pDOMNode->getNodeValue())<< endl;
                }
                else
                {
                    cout << "pDOMNode->getNodeValue() is NULL." << endl;
                }
            }
        }
    }
    */
    //写入到文件
    Serializer->write( doc , Output );


    delete Targe;


    Output->release();


    doc->release();


    Serializer->release();


    XMLPlatformUtils::Terminate();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值