Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535942
  • 博文数量: 55
  • 博客积分: 1520
  • 博客等级: 上尉
  • 技术积分: 661
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-19 22:01
文章存档

2013年(1)

2012年(1)

2011年(7)

2010年(16)

2009年(30)

我的朋友

分类: C/C++

2010-03-01 11:52:10

代码如下:
/*
 * ===========================================================================
 *
 *       Filename:  ParseXML.cpp
 *
 *    Description:  This is an example of the use of Xerces-C++ operation XML.
 *
 *        Version:  1.0
 *        Created:  03/01/2010 09:44:19 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  huabo (caodaijun), caodaijun@feinno.com
 *        Company:  feinno
 *
 * ===========================================================================
 */
#include
#include
#include
#include
#include
#include
#include
#include

#include

#if defined(XERCES_NEW_IOSTREAMS)
#include
#else
#include
#endif

XERCES_CPP_NAMESPACE_USE
class XStr
{
        public:
                XStr(const char* const toTranscode)
                {
                        fUnicodeForm = XMLString::transcode(toTranscode);
                }
                ~XStr()
                {
                        XMLString::release(&fUnicodeForm);
                }
                const XMLCh* unicodeForm() const
                {
                        return fUnicodeForm;
                }
        private:
                XMLCh*   fUnicodeForm;
};

#define X(str) XStr(str).unicodeForm()

std::string xml_string =
"100000abcd
";

std::string basic_string = "good";

int main(int argC, char*argV[])
{
        // Initialize the XML4C2 system.
        try
        {
                XMLPlatformUtils::Initialize();
        }
        catch(const XMLException& toCatch)
        {
                char *pMsg = XMLString::transcode(toCatch.getMessage());
                XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n"
                        << "  Exception message:"
                        << pMsg;
                XMLString::release(&pMsg);
                return 1;
        }

        DOMImplementation* impl =  DOMImplementationRegistry::getDOMImplementation(X("LS"));
        DOMLSSerializer*   theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
        DOMLSOutput       *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
        MemBufFormatTarget* target = new MemBufFormatTarget();
        theOutputDesc->setByteStream(target);

        if (impl != NULL)
        {
                try
                {

                        XercesDOMParser* parser = new XercesDOMParser();

                        MemBufInputSource* xml_mem = new MemBufInputSource(
                                        (const XMLByte* )xml_string.c_str(),
                                        xml_string.length(),
                                        "xml",
                                        false);
                        parser->parse( *xml_mem );
                        DOMDocument* xml_doc = parser->getDocument();
                        DOMElement* xml_root = xml_doc->getDocumentElement();
                        if (NULL == xml_root)
                        {
                                std::cout<< "Empty XML document!\n";
                                exit(1);
                        }
                        //输出整个文档
                        std::cout<< XMLString::transcode(theSerializer->writeToString((DOMNode* )xml_root))<< std::endl;

                        //输出XPATH指定的结点
                        DOMXPathNSResolver* resolver = xml_doc->createNSResolver(xml_root);
                        DOMXPathResult* result = xml_doc->evaluate(
                                        X("/corporation/basic"),
                                        xml_root,
                                        resolver,
                                        DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
                                        NULL);
                        result->snapshotItem(0);
                        DOMNode*  curentNode = result->getNodeValue();
                        DOMNode*  prevNode = curentNode->getParentNode();
                        theSerializer->write(curentNode, theOutputDesc);
                        std::cout<< target->getRawBuffer()<< std::endl;

                        //replace the basic child
                        MemBufInputSource* basic_mem = new MemBufInputSource(
                                        (const XMLByte* )basic_string.c_str(),
                                        basic_string.length(),
                                        "basic",
                                        false);
                        parser->parse( *basic_mem );
                        DOMDocument* basic_doc = parser->getDocument();
                        DOMElement* basic_root = basic_doc->getDocumentElement();
                        DOMNode* newnode = xml_doc->importNode((DOMNode* )basic_root, true);
                        prevNode->replaceChild(newnode, curentNode);

                        //再次输出整个文档
                        std::cout<< XMLString::transcode(theSerializer->writeToString((DOMNode* )xml_root))<< std::endl;

                        delete basic_mem;
                        delete xml_mem;
                        delete target;
                        delete parser;
                        result->release();
                        resolver->release();
                        theOutputDesc->release();
                        theSerializer->release();
                }
                catch(const OutOfMemoryException&)
                {
                        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
                }
                catch (const DOMException& e)
                {
                        XERCES_STD_QUALIFIER cerr << "DOMException code is:  " << e.code << XERCES_STD_QUALIFIER endl;
                }
                catch (...)
                {
                        XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl;
                }
        }
        else
        {
                XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl;
        }

        XMLPlatformUtils::Terminate();
        return 0;
}
阅读(4489) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

xiejanee2014-08-15 16:44:35

楼主:你好!我想请问下  你在代码中用DOMDocument* xml_doc = parser->getDocument();语句创建了doc 但是最后在清理内存的时候为什么没有将doc所指向的内存空间释放?
 我在处理xml文件时遇到了一个问题 就是自己写的一个类中需要实现对xml文件的修改以及保存 所以代码中用到了xerces的dom方式对xml文件进行修改  代码中有paser以及doc的创建语句 :
XercesDOMParser *parser = new XercesDOMParser;
xercesc_3_1::DOMDocument *doc = parser->getDocument();
在xml文件处理完后,想同时释放类对象中parser 以及 doc所指向的内存空间,此时就会报内存读取错误, 我是用doc->release();delete parser;这两条语句对两者进行空间清理的,但是若只调用其中的一个语句对相应指针的指向空间进行清理时,就不会报错,难道pa