Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Make the most of Xerces-C++, Part 2

A DOM implementation

Return to article.


Listing 10. Creating a formatter for serialization
                #include "XercesString.h"

void save_to_file(DOMDocument *pDoc, const char *strPath)
{
  XercesString wstrPath( XMLString::transcode(strPath) );
  if (wstrPath)
  {
    DOMWriterImpl writer;
    LocalFileFormatTarget fileTarget(wstrPath);

    // write the resulting document.
    writer.setEncoding( XercesString("UTF-8") );
    writer.writeNode(&fileTarget, *pDoc);
  }
}

void dump_xml(DOMDocument *pDoc)
{
  DOMImplementation *pImplement = NULL;
  // these two are needed to display DOM output.
  DOMWriter *pSerializer = NULL;
  XMLFormatTarget *pTarget = NULL;

  // get a serializer, an instance of DOMWriter (the "LS" stands for load-save).
  pImplement = DOMImplementationRegistry::getDOMImplementation(XercesString("LS"));
  pSerializer = ( (DOMImplementationLS*)pImplement )->createDOMWriter();
  pTarget = new StdOutFormatTarget();
  // set user specified end of line sequence and output encoding
  pSerializer->setNewLine( XercesString("\n") );

  // set feature if the serializer supports the feature/mode
  if ( pSerializer->canSetFeature(XMLUni::fgDOMWRTSplitCdataSections, false) )
  	pSerializer->setFeature(XMLUni::fgDOMWRTSplitCdataSections, false);

  if ( pSerializer->canSetFeature(XMLUni::fgDOMWRTDiscardDefaultContent, false) )
  	pSerializer->setFeature(XMLUni::fgDOMWRTDiscardDefaultContent, false);

  // turn off serializer "pretty print" option
  if ( pSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, false) )
  	pSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, false);

  if ( pSerializer->canSetFeature(XMLUni::fgDOMWRTBOM, false) )
  	pSerializer->setFeature(XMLUni::fgDOMWRTBOM, false);

  pSerializer->writeNode(pTarget, *pDoc);

}

Return to article.