Solved

how do you write a DOMDocument to an XML file in C++

Posted on 2004-04-21
5
443 Views
Last Modified: 2007-12-19
I'm using xercesc to build up my DOMDocument. This is the easy part. Now I'm trying to write this DOM to an XML file but I can't find anyway to do it. I know in java I was able to use the Transformer class. Is there anything in C++ similar? Or do I have to write each node one by one? I would hope there is some class that would do it nicely....

Thanks,
Paul
0
Comment
Question by:pwellar
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
dualsoul earned 125 total points
ID: 10886208
you can use DOMWriter.
this is the sample form official XercerC docs:

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

    int serializeDOM(DOMNode* node) {

        XMLCh tempStr[100];
        XMLString::transcode("LS", tempStr, 99);
        DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
        DOMWriter* theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter();

        // optionally you can set some features on this serializer
        if (theSerializer->canSetFeature(XMLUni::fgDOMWRTDiscardDefaultContent, true))
            theSerializer->setFeature(XMLUni::fgDOMWRTDiscardDefaultContent, true);

        if (theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true))
             theSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);

        // optionally you can implement your DOMWriterFilter (e.g. MyDOMWriterFilter)
        // and set it to the serializer
        DOMWriterFilter* myFilter = new myDOMWriterFilter();
        theSerializer->setFilter(myFilter);

        // optionally you can implement your DOMErrorHandler (e.g. MyDOMErrorHandler)
        // and set it to the serializer
        DOMErrorHandler* errHandler = new myDOMErrorHandler();
        theSerializer->setErrorHandler(myErrorHandler);

        // StdOutFormatTarget prints the resultant XML stream
        // to stdout once it receives any thing from the serializer.
        XMLFormatTarget *myFormTarget = new StdOutFormatTarget();

        try {
            // do the serialization through DOMWriter::writeNode();
            theSerializer->writeNode(myFormTarget, *node);
        }
        catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);
            return -1;
        }
        catch (const DOMException& toCatch) {
            char* message = XMLString::transcode(toCatch.msg);
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);
            return -1;
        }
        catch (...) {
            cout << "Unexpected Exception \n" ;
            return -1;
        }


        theSerializer->release();
        delete myErrorHandler;
        delete myFilter;
        delete myFormTarget;
        return 0;
    }
.............................................................................

hope it helps.
0
 

Author Comment

by:pwellar
ID: 10890929
I did see that before, this is writing it per node. I was hoping that there was something to write it per DOMDocument. Or does this support in writing out the whole structure? If so what do you pass into the writeNode call?

Thanks!
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 10896420
try to pass RootNode to writeNode.
if this doen't help, i think it's rather easier to iterate over all nodes and save them :)
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question