Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-21
5
Medium Priority
?
476 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 500 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

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…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

715 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