Solved

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

Posted on 2004-04-21
5
448 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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