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 8. DOM loading and exception boilerplate
                #include <xercesc/dom/DOM.hpp>
#include "XercesString.h"

// boilerplate DOM loading example.
int main(int argc, char *argv[])
{
  bool bFailed = false;
  if (argc < 2) 
    return -1;

  // initialize the XML library.
  XMLPlatformUtils::Initialize();
  // create new parser instance.
  XercesDOMParser *parser = new XercesDOMParser;
  if (parser)
  {
    parser->setValidationScheme(XercesDOMParser::Val_Auto);
    parser->setDoNamespaces(false);
    parser->setDoSchema(false);

	// skip this if you haven't written your own error reporter class.
	DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter;
	parser->setErrorHandler(errReporter);

    parser->setCreateEntityReferenceNodes(false);
    //parser->setToCreateXMLDeclTypeNode(true);
    try
    {
      XercesString src(argv[1]);
	  LocalFileInputSource source(src);
      parser->parse(source);
      bFailed = parser->getErrorCount() != 0;
      if (bFailed)
      {
        std::cerr << "Parsing " << argv[1];
        std::cerr << " error count: " << parser->getErrorCount() << std::endl;
      }
    }
    catch (const DOMException& e)
    {
      std::cerr << "DOM Exception parsing ";
      std::cerr << strSrc;
      std::cerr << " reports: ";
	  // was message provided?
	  if (e.msg)
	  {
		// yes: display it as ascii.
		char *strMsg = XMLString::transcode(e.msg);
        std::cerr << strMsg << std::endl;
		XMLString::release(&strMsg);
	  }
	  else
	    // no: just display the error code.
        std::cerr << e.code << std::endl;

	  bFailed = true;
	}
    catch (const XMLException& e)
    {
      std::cerr << "XML Exception parsing ";
      std::cerr << argv[1];
      std::cerr << " reports: ";
      std::cerr << e.getMessage() << std::endl;
	  bFailed = true;
    }
    catch (const SAXException& e)
    {
      std::cerr << "SAX Exception parsing ";
      std::cerr << argv[1];
      std::cerr << " reports: ";
      std::cerr << e.getMessage() << std::endl;
	  bFailed = true;
    }
    catch (...)
    {
      std::cerr << "An exception parsing ";
      std::cerr << argv[1] << std::endl;
	  bFailed = true;
    }
    // did the input document parse okay?
    if (!bFailed)
	{
	  DOMNode *pDoc = parser->getDocument();
	  // insert code to do something with the DOM document here.
	}
  }
  XMLPlatformUtils::Terminate();
  getchar();
  return 0;
}

Return to article.