You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ne...@apache.org on 2003/12/11 00:48:53 UTC

cvs commit: xml-xerces/c/samples/CreateDOMDocument CreateDOMDocument.cpp

neilg       2003/12/10 15:48:53

  Modified:    c/samples/CreateDOMDocument CreateDOMDocument.cpp
  Log:
  make CreateDOMDocument sample more robust; thanks to Steve Dulin
  
  Revision  Changes    Path
  1.18      +76 -55    xml-xerces/c/samples/CreateDOMDocument/CreateDOMDocument.cpp
  
  Index: CreateDOMDocument.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/samples/CreateDOMDocument/CreateDOMDocument.cpp,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- CreateDOMDocument.cpp	7 Aug 2003 21:21:38 -0000	1.17
  +++ CreateDOMDocument.cpp	10 Dec 2003 23:48:53 -0000	1.18
  @@ -1,7 +1,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -151,7 +151,7 @@
                   "    CreateDOMDocument\n\n"
                   "This program creates a new DOM document from scratch in memory.\n"
                   "It then prints the count of elements in the tree.\n"
  -             <<  XERCES_STD_QUALIFIER endl;
  +             << XERCES_STD_QUALIFIER endl;
           errorCode = 1;
       }
       if(errorCode) {
  @@ -159,59 +159,80 @@
           return errorCode;
       }
   
  -    {
  -        //  Nest entire test in an inner block.
  -        //  The tree we create below is the same that the XercesDOMParser would
  -        //  have created, except that no whitespace text nodes would be created.
  -
  -        // <company>
  -        //     <product>Xerces-C</product>
  -        //     <category idea='great'>XML Parsing Tools</category>
  -        //     <developedBy>Apache Software Foundation</developedBy>
  -        // </company>
  -
  -        DOMImplementation* impl =  DOMImplementationRegistry::getDOMImplementation(X("Core"));
  -
  -        DOMDocument* doc = impl->createDocument(
  -                    0,                    // root element namespace URI.
  -                    X("company"),         // root element name
  -                    0);                   // document type object (DTD).
  -
  -        DOMElement* rootElem = doc->getDocumentElement();
  -
  -        DOMElement*  prodElem = doc->createElement(X("product"));
  -        rootElem->appendChild(prodElem);
  -
  -        DOMText*    prodDataVal = doc->createTextNode(X("Xerces-C"));
  -        prodElem->appendChild(prodDataVal);
  -
  -        DOMElement*  catElem = doc->createElement(X("category"));
  -        rootElem->appendChild(catElem);
  -
  -        catElem->setAttribute(X("idea"), X("great"));
  -
  -        DOMText*    catDataVal = doc->createTextNode(X("XML Parsing Tools"));
  -        catElem->appendChild(catDataVal);
  -
  -        DOMElement*  devByElem = doc->createElement(X("developedBy"));
  -        rootElem->appendChild(devByElem);
  -
  -        DOMText*    devByDataVal = doc->createTextNode(X("Apache Software Foundation"));
  -        devByElem->appendChild(devByDataVal);
  -
  -        //
  -        // Now count the number of elements in the above DOM tree.
  -        //
  -
  -        unsigned int elementCount = doc->getElementsByTagName(X("*"))->getLength();
  -        XERCES_STD_QUALIFIER cout << "The tree just created contains: " << elementCount
  -             << " elements." << XERCES_STD_QUALIFIER endl;
  -
  -        doc->release();
  -
  +   {
  +       //  Nest entire test in an inner block.
  +       //  The tree we create below is the same that the XercesDOMParser would
  +       //  have created, except that no whitespace text nodes would be created.
  +
  +       // <company>
  +       //     <product>Xerces-C</product>
  +       //     <category idea='great'>XML Parsing Tools</category>
  +       //     <developedBy>Apache Software Foundation</developedBy>
  +       // </company>
  +
  +       DOMImplementation* impl =  DOMImplementationRegistry::getDOMImplementation(X("Core"));
  +
  +       if (impl != NULL)
  +       {
  +           try
  +           {
  +               DOMDocument* doc = impl->createDocument(
  +                           0,                    // root element namespace URI.
  +                           X("company"),         // root element name
  +                           0);                   // document type object (DTD).
  +
  +               DOMElement* rootElem = doc->getDocumentElement();
  +
  +               DOMElement*  prodElem = doc->createElement(X("product"));
  +               rootElem->appendChild(prodElem);
  +
  +               DOMText*    prodDataVal = doc->createTextNode(X("Xerces-C"));
  +               prodElem->appendChild(prodDataVal);
  +
  +               DOMElement*  catElem = doc->createElement(X("category"));
  +               rootElem->appendChild(catElem);
  +
  +               catElem->setAttribute(X("idea"), X("great"));
  +
  +               DOMText*    catDataVal = doc->createTextNode(X("XML Parsing Tools"));
  +               catElem->appendChild(catDataVal);
  +
  +               DOMElement*  devByElem = doc->createElement(X("developedBy"));
  +               rootElem->appendChild(devByElem);
  +
  +               DOMText*    devByDataVal = doc->createTextNode(X("Apache Software Foundation"));
  +               devByElem->appendChild(devByDataVal);
  +
  +               //
  +               // Now count the number of elements in the above DOM tree.
  +               //
  +
  +               unsigned int elementCount = doc->getElementsByTagName(X("*"))->getLength();
  +               XERCES_STD_QUALIFIER cout << "The tree just created contains: " << elementCount
  +                    << " elements." << XERCES_STD_QUALIFIER endl;
  +
  +               doc->release();
  +           }
  +
  +           catch (const DOMException& e)
  +           {
  +               XERCES_STD_QUALIFIER cerr << "DOMException code is:  " << e.code << XERCES_STD_QUALIFIER endl;
  +               errorCode = 2;
  +           }
  +           catch (...)
  +           {
  +               XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl;
  +               errorCode = 3;
  +           }
  +       }  // (inpl != NULL)
  +       else
  +       {
  +           XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl;
  +           errorCode = 4;
  +       }
      }
   
  -    XMLPlatformUtils::Terminate();
  -    return 0;
  +   XMLPlatformUtils::Terminate();
  +   return errorCode;
   }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org