#include "xmlBase/XmlParser.h"
#include "xmlBase/Dom.h"
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/dom/DOMNodeList.hpp>
#include "facilities/Util.h"
#include <string>
#include <iostream>
#include <fstream>
Go to the source code of this file.
Functions | |
int | main (int argc, char *argv[]) |
int main | ( | int | argc, | |
char * | argv[] | |||
) |
Two arguments may be supplied for input and output file. First argument defaults to $XMLBASEROOT/xml/test.xml Second argument defaults to no output file at all. If supplied, the value of "-" is taken to mean standard output. Anything else is assumed to be a filename.
Definition at line 21 of file main.cxx.
References check_raw_filter::filename, xmlBase::Dom::findFirstChildByName(), xmlBase::Dom::getDoubleAttribute(), xmlBase::Dom::getDoublesAttribute(), xmlBase::Dom::getIntAttribute(), xmlBase::Dom::getIntsAttribute(), xmlBase::DomException::getMsg(), xmlBase::ParseException::getMsg(), xmlBase::XmlParser::parse(), xmlBase::Dom::prettyPrintElement(), xmlBase::Dom::printElement(), and deljobs::string.
00021 { 00022 XERCES_CPP_NAMESPACE_USE 00023 00024 std::string infile; 00025 if (argc < 2) { 00026 infile=std::string("$(XMLBASEROOT)/xml/test.xml"); 00027 } 00028 else { 00029 infile = std::string(argv[1]); 00030 } 00031 00032 facilities::Util::expandEnvVar(&infile); 00033 00034 xmlBase::XmlParser* parser = new xmlBase::XmlParser(true); 00035 00036 DOMDocument* doc = 0; 00037 try { 00038 doc = parser->parse(infile.c_str()); 00039 } 00040 catch (xmlBase::ParseException ex) { 00041 std::cout << "caught exception with message " << std::endl; 00042 std::cout << ex.getMsg() << std::endl; 00043 delete parser; 00044 return 0; 00045 } 00046 00047 if (doc != 0) { // successful 00048 std::cout << "Document successfully parsed" << std::endl; 00049 00050 // look up some attributes 00051 DOMElement* docElt = doc->getDocumentElement(); 00052 DOMElement* attElt = 00053 xmlBase::Dom::findFirstChildByName(docElt, "ChildWithAttributes"); 00054 double doubleVal; 00055 int intVal; 00056 00057 try { 00058 intVal = xmlBase::Dom::getIntAttribute(attElt, "goodInt"); 00059 std::cout << "goodInt value was " << intVal << std::endl << std::endl; 00060 } 00061 catch (xmlBase::DomException ex) { 00062 std::cout << std::endl << "DomException: " << ex.getMsg() 00063 << std::endl << std::endl; 00064 } 00065 00066 try { 00067 std::vector<int> ints; 00068 unsigned nInts = xmlBase::Dom::getIntsAttribute(attElt, "goodInts", ints); 00069 std::cout << "Found " << nInts << " goodInts: " << std::endl; 00070 for (unsigned iInt=0; iInt < nInts; iInt++) { 00071 std::cout << ints[iInt] << " "; 00072 } 00073 std::cout << std::endl << std::endl; 00074 } 00075 catch (xmlBase::DomException ex) { 00076 std::cout << std::endl << "DomException processing goodInts: " 00077 << ex.getMsg() << std::endl << std::endl; 00078 } 00079 00080 try { 00081 std::vector<double> doubles; 00082 unsigned nD = xmlBase::Dom::getDoublesAttribute(attElt, "goodDoubles", 00083 doubles); 00084 std::cout << "Found " << nD << " goodDoubles: " << std::endl; 00085 for (unsigned iD=0; iD < nD; iD++) { 00086 std::cout << doubles[iD] << " "; 00087 } 00088 std::cout << std::endl << std::endl; 00089 } 00090 catch (xmlBase::DomException ex) { 00091 std::cout << std::endl << "DomException processing goodDoubles: " 00092 << ex.getMsg() << std::endl << std::endl; 00093 } 00094 00095 00096 00097 try { 00098 intVal = xmlBase::Dom::getIntAttribute(attElt, "badInt"); 00099 std::cout << "badInt value was " << intVal << std::endl << std::endl; 00100 } 00101 catch (xmlBase::DomException ex) { 00102 std::cout << std::endl << "DomException: " << ex.getMsg() 00103 << std::endl << std::endl; 00104 } 00105 00106 try { 00107 doubleVal = xmlBase::Dom::getDoubleAttribute(attElt, "goodDouble"); 00108 std::cout << "goodDouble value was " << doubleVal 00109 << std::endl << std::endl; 00110 } 00111 catch (xmlBase::DomException ex) { 00112 std::cout << std::endl << "DomException: " << ex.getMsg() 00113 << std::endl << std::endl; 00114 } 00115 00116 try { 00117 doubleVal = xmlBase::Dom::getDoubleAttribute(attElt, "badDouble"); 00118 std::cout << std::endl << "badDouble value was " << doubleVal 00119 << std::endl << std::endl; 00120 } 00121 catch (xmlBase::DomException ex) { 00122 std::cout << std::endl << "DomException: " << ex.getMsg() 00123 << std::endl << std::endl; 00124 } 00125 00126 00127 try { 00128 std::vector<double> doubles; 00129 unsigned nD = xmlBase::Dom::getDoublesAttribute(attElt, "badDoubles", 00130 doubles); 00131 std::cout << "Found " << nD << " badDoubles: " << std::endl; 00132 for (unsigned iD=0; iD < nD; iD++) { 00133 std::cout << doubles[iD] << " "; 00134 } 00135 std::cout << std::endl << std::endl; 00136 } 00137 catch (xmlBase::DomException ex) { 00138 std::cout << std::endl << "DomException processing badDoubles: " 00139 << ex.getMsg() 00140 << std::endl << std::endl; 00141 } 00142 00143 if (argc > 2) { // attempt to output 00144 char *hyphen = "-"; 00145 00146 std::ostream* out; 00147 00148 if (*(argv[2]) == *hyphen) { 00149 out = &std::cout; 00150 } 00151 else { // try to open file as ostream 00152 char *filename = argv[2]; 00153 out = new std::ofstream(filename); 00154 } 00155 *out << "Document source: " << std::string(argv[1]) << std::endl; 00156 *out << std::endl << "Straight print of document:" << std::endl; 00157 xmlBase::Dom::printElement(docElt, *out); 00158 *out << std::endl << std::endl << "Add indentation and line breaks:" 00159 << std::endl; 00160 xmlBase::Dom::prettyPrintElement(docElt, *out, ""); 00161 } 00162 } 00163 delete parser; 00164 return(0); 00165 }