testXerces.cc

Go to the documentation of this file.
00001 #include <xercesc/parsers/XercesDOMParser.hpp>
00002 #include <xercesc/dom/DOM.hpp>
00003 #include <xercesc/sax/HandlerBase.hpp>
00004 #include <xercesc/util/XMLString.hpp>
00005 #include <xercesc/util/PlatformUtils.hpp>
00006 #include <string>
00007 
00008 #if defined(XERCES_NEW_IOSTREAMS)
00009 #include <iostream>
00010 #else
00011 #include <iostream.h>
00012 #endif
00013 
00014 XERCES_CPP_NAMESPACE_USE
00015 using namespace std;
00016 
00017 
00018 int main (int argc, char* args[]) {
00019     try {
00020         XMLPlatformUtils::Initialize();
00021     }
00022     catch (const XMLException& toCatch) {
00023         char* message = XMLString::transcode(toCatch.getMessage());
00024         cout << "Error during initialization! :\n" << message << endl;
00025         XMLString::release(&message);
00026         return 1;
00027     }
00028 
00029     XercesDOMParser* parser = new XercesDOMParser();
00030     parser->setValidationScheme(XercesDOMParser::Val_Always); // optional
00031     parser->setDoNamespaces(true); // optional
00032 
00033     ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
00034     parser->setErrorHandler(errHandler);
00035 
00036     string xmlFile = "test.xml";
00037     if (argc == 2) xmlFile = args[1];
00038     try {
00039         parser->parse(xmlFile.c_str());
00040     }
00041     catch (const XMLException& toCatch) {
00042         char* message = XMLString::transcode(toCatch.getMessage());
00043         cout << "Exception message is: \n" << message << endl;
00044         XMLString::release(&message);
00045         return -1;
00046     }
00047     catch (const DOMException& toCatch) {
00048         char* message = XMLString::transcode(toCatch.msg);
00049         cout << "Exception message is: \n" << message << endl;
00050         XMLString::release(&message);
00051         return -1;
00052     }
00053     catch (...) {
00054       cout << "Unexpected Exception - does " << xmlFile << " exist?" << endl;
00055         return -1;
00056     }
00057 
00058     DOMNode* doc = parser->getDocument();
00059     if (doc) {
00060       DOMNodeList* nodes = doc->getChildNodes();
00061       if (nodes) {
00062         cout << "Num nodes = " << nodes->getLength() << endl;
00063         for (XMLSize_t i = 0; i < nodes->getLength(); ++i) {
00064           DOMNode* node = nodes->item(i);
00065           if (node) {
00066             cout << i << ": " << *( node->getNodeName() ) << endl;
00067             cout << "Children: " << node->getChildNodes()->getLength() << endl;
00068             for (XMLSize_t j = 0; i < node->getChildNodes()->getLength(); ++i) {
00069               DOMNode* node2 = node->getChildNodes()->item(j);
00070               if (node2) cout << j << ": " << *( node2->getNodeValue() ) << endl;
00071             }
00072           }
00073         }
00074       }
00075     }
00076 
00077     delete parser;
00078     delete errHandler;
00079     return EXIT_SUCCESS;
00080 }
00081 
00082 // #include <xercesc/util/PlatformUtils.hpp>
00083 // #include <xercesc/dom/DOM.hpp>
00084 
00085 // XERCES_CPP_NAMESPACE_USE 
00086   
00087 // int main(int argc, char* argv[])
00088 // {
00089 //   try {
00090 //     XMLPlatformUtils::Initialize();
00091 //   }
00092 //   catch (const XMLException& toCatch) {
00093 //     // Do your failure processing here
00094 //     return 1;
00095 //   }
00096 
00097 //   // Do your actual work with Xerces-C++ here.
00098 
00099 //   XMLPlatformUtils::Terminate();
00100 
00101 //   // Other terminations and cleanup.
00102 //   return EXIT_SUCCESS;
00103 // }
00104