Xerces:如何创建 DOM 文档

问题描述

我想用 xerces 创建一个 DOMDocument,但是我得到一个空指针的错误

此时:

DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr,0);

错误是:

xercesc_3_2::domImplementation 为 nullptr

我的代码是这样的:

//
    //  Create a small document tree
    //

    {
        XMLCh tempStr[100];

        XMLString::transcode("Range",tempStr,99);
        DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr,0);

        XMLString::transcode("root",99);
        DOMDocument*   doc = impl->createDocument(0,0);
        DOMElement*   root = doc->getDocumentElement();

        XMLString::transcode("FirstElement",99);
        DOMElement*   e1 = doc->createElement(tempStr);
        root->appendChild(e1);

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

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

        // 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
        DOMElement* removedElement = root->removeChild(e2);
        removedElement->release();

        // no need to release this returned object which is owned by implementation
        XMLString::transcode("*",99);
        DOMNodeList*    nodeList = doc->getElementsByTagName(tempStr);

        // done with the document,must call release() to release the entire document resources
        doc->release();
    };
         

取自https://xerces.apache.org/xerces-c/program-dom-3.html

我错在哪里?我该如何解决这个问题?

谢谢

解决方法

xercesc_3_2::XMLPlatformUtils::Initialize();

    xercesc_3_2::DOMDocument* domDocument = NULL;
    xercesc_3_2::DOMDocumentType* doc_domDocument = NULL;
    xercesc_3_2::DOMElement* root;
    xercesc_3_2::DOMElement* child1;
    xercesc_3_2::DOMElement* child2;
    xercesc_3_2::DOMImplementation* domImplementation = NULL;

    domImplementation = 
        xercesc_3_2::DOMImplementationRegistry::getDOMImplementation(xercesc_3_2::XMLString::transcode(""));

    if (domImplementation == NULL) {
        cout << "Vuoto" << endl;
    }

    domDocument = domImplementation->createDocument(0,xercesc_3_2::XMLString::transcode("xml"),doc_domDocument);
    root = domDocument->getDocumentElement();

    child1 = domDocument->createElement(xercesc_3_2::XMLString::transcode("Document"));
    root->appendChild(child1);
    cout << "debug node: " << xercesc_3_2::XMLString::transcode(child1->getTagName()) << endl;

    child2 = domDocument->createElement(xercesc_3_2::XMLString::transcode("name"));
    child2->appendChild(domDocument->createTextNode(xercesc_3_2::XMLString::transcode("1")));
    child1->appendChild(child2);
    cout << "debug node: " << xercesc_3_2::XMLString::transcode(child2->getTagName()) << endl;

    child2 = domDocument->createElement(xercesc_3_2::XMLString::transcode("styleUrl"));
    child2->appendChild(domDocument->createTextNode(xercesc_3_2::XMLString::transcode("0")));
    child1->appendChild(child2);
    cout << "debug node: " << xercesc_3_2::XMLString::transcode(child2->getTagName()) << endl;

并写入文件:

const int ABSOLUTE_PATH_FILENAME_PREFIX_SIZE = 9;

    //Return the first registered implementation that has the desired features. In this case,we are after a DOM implementation that has the LS feature... or Load/Save.
    xercesc_3_2::DOMImplementation* implementation = xercesc_3_2::DOMImplementationRegistry::getDOMImplementation(xercesc_3_2::XMLString::transcode("LS"));

    // Create a DOMLSSerializer which is used to serialize a DOM tree into an XML document.
    xercesc_3_2::DOMLSSerializer* serializer = ((xercesc_3_2::DOMImplementationLS*)implementation)->createLSSerializer();

    // Make the output more human readable by inserting line feeds.
    if (serializer->getDomConfig()->canSetParameter(xercesc_3_2::XMLUni::fgDOMWRTFormatPrettyPrint,true))
        serializer->getDomConfig()->setParameter(xercesc_3_2::XMLUni::fgDOMWRTFormatPrettyPrint,true);

    // The end-of-line sequence of characters to be used in the XML being written out. 
    serializer->setNewLine(xercesc_3_2::XMLString::transcode("\r\n"));

    // Convert the path into Xerces compatible XMLCh*.
    XMLCh* tempFilePath = xercesc_3_2::XMLString::transcode(fileName.c_str());

    // Specify the target for the XML output.
    xercesc_3_2::XMLFormatTarget* formatTarget = new xercesc_3_2::LocalFileFormatTarget(tempFilePath);
    //XMLFormatTarget *myFormTarget = new StdOutFormatTarget();

    // Create a new empty output destination object.
    xercesc_3_2::DOMLSOutput* output = ((xercesc_3_2::DOMImplementationLS*)implementation)->createLSOutput();

    // Set the stream to our target.
    output->setByteStream(formatTarget);

    // Write the serialized output to the destination.
    serializer->write(domDocument,output);

    // Cleanup.
    serializer->release();
    xercesc_3_2::XMLString::release(&tempFilePath);
    delete formatTarget;
    output->release();