c++xmlxerces-c

Xerces: How to create DOM document


I want to create a DOMDocument with xerces, but I get an error of null pointer

at this point:

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

The error is:

xercesc_3_2::domImplementation was nullptr

My code is this:

//
    //  Create a small document tree
    //

    {
        XMLCh tempStr[100];

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

        XMLString::transcode("root", tempStr, 99);
        DOMDocument*   doc = impl->createDocument(0, tempStr, 0);
        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);
        root->appendChild(e2);

        XMLString::transcode("aTextNode", tempStr, 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("*", tempStr, 99);
        DOMNodeList*    nodeList = doc->getElementsByTagName(tempStr);

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

taken from https://xerces.apache.org/xerces-c/program-dom-3.html

Where I wrong? How can I solve this problem?

Thank you


Solution

  • 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;
    

    and write on file:

    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();