BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/xmlBase/src/main.cxx
Go to the documentation of this file.
1/// Test program for xmlBase facility. Parse xml file and optionally
2/// write it out to a stream.
3
4#include "facilities/Util.h"
5#include "xmlBase/Dom.h"
6#include "xmlBase/XmlParser.h"
7#include <xercesc/dom/DOMElement.hpp>
8#include <xercesc/dom/DOMNodeList.hpp>
9
10#include <fstream>
11#include <iostream>
12#include <string>
13
14/**
15 Two arguments may be supplied for input and output file.
16 * First argument defaults to $XMLBASEROOT/xml/test.xml
17 * Second argument defaults to no output file at all. If supplied,
18 the value of "-" is taken to mean standard output. Anything
19 else is assumed to be a filename.
20*/
21int main( int argc, char* argv[] ) {
22 XERCES_CPP_NAMESPACE_USE
23
24 std::string infile;
25 if ( argc < 2 ) { infile = std::string( "$(XMLBASEROOT)/xml/test.xml" ); }
26 else { infile = std::string( argv[1] ); }
27
29
30 xmlBase::XmlParser* parser = new xmlBase::XmlParser( true );
31
32 DOMDocument* doc = 0;
33 try
34 { doc = parser->parse( infile.c_str() ); } catch ( xmlBase::ParseException ex )
35 {
36 std::cout << "caught exception with message " << std::endl;
37 std::cout << ex.getMsg() << std::endl;
38 delete parser;
39 return 0;
40 }
41
42 if ( doc != 0 )
43 { // successful
44 std::cout << "Document successfully parsed" << std::endl;
45
46 // look up some attributes
47 DOMElement* docElt = doc->getDocumentElement();
48 DOMElement* attElt = xmlBase::Dom::findFirstChildByName( docElt, "ChildWithAttributes" );
49 double doubleVal;
50 int intVal;
51
52 try
53 {
54 intVal = xmlBase::Dom::getIntAttribute( attElt, "goodInt" );
55 std::cout << "goodInt value was " << intVal << std::endl << std::endl;
56 } catch ( xmlBase::DomException ex )
57 { std::cout << std::endl << "DomException: " << ex.getMsg() << std::endl << std::endl; }
58
59 try
60 {
61 std::vector<int> ints;
62 unsigned nInts = xmlBase::Dom::getIntsAttribute( attElt, "goodInts", ints );
63 std::cout << "Found " << nInts << " goodInts: " << std::endl;
64 for ( unsigned iInt = 0; iInt < nInts; iInt++ ) { std::cout << ints[iInt] << " "; }
65 std::cout << std::endl << std::endl;
66 } catch ( xmlBase::DomException ex )
67 {
68 std::cout << std::endl
69 << "DomException processing goodInts: " << ex.getMsg() << std::endl
70 << std::endl;
71 }
72
73 try
74 {
75 std::vector<double> doubles;
76 unsigned nD = xmlBase::Dom::getDoublesAttribute( attElt, "goodDoubles", doubles );
77 std::cout << "Found " << nD << " goodDoubles: " << std::endl;
78 for ( unsigned iD = 0; iD < nD; iD++ ) { std::cout << doubles[iD] << " "; }
79 std::cout << std::endl << std::endl;
80 } catch ( xmlBase::DomException ex )
81 {
82 std::cout << std::endl
83 << "DomException processing goodDoubles: " << ex.getMsg() << std::endl
84 << std::endl;
85 }
86
87 try
88 {
89 intVal = xmlBase::Dom::getIntAttribute( attElt, "badInt" );
90 std::cout << "badInt value was " << intVal << std::endl << std::endl;
91 } catch ( xmlBase::DomException ex )
92 { std::cout << std::endl << "DomException: " << ex.getMsg() << std::endl << std::endl; }
93
94 try
95 {
96 doubleVal = xmlBase::Dom::getDoubleAttribute( attElt, "goodDouble" );
97 std::cout << "goodDouble value was " << doubleVal << std::endl << std::endl;
98 } catch ( xmlBase::DomException ex )
99 { std::cout << std::endl << "DomException: " << ex.getMsg() << std::endl << std::endl; }
100
101 try
102 {
103 doubleVal = xmlBase::Dom::getDoubleAttribute( attElt, "badDouble" );
104 std::cout << std::endl << "badDouble value was " << doubleVal << std::endl << std::endl;
105 } catch ( xmlBase::DomException ex )
106 { std::cout << std::endl << "DomException: " << ex.getMsg() << std::endl << std::endl; }
107
108 try
109 {
110 std::vector<double> doubles;
111 unsigned nD = xmlBase::Dom::getDoublesAttribute( attElt, "badDoubles", doubles );
112 std::cout << "Found " << nD << " badDoubles: " << std::endl;
113 for ( unsigned iD = 0; iD < nD; iD++ ) { std::cout << doubles[iD] << " "; }
114 std::cout << std::endl << std::endl;
115 } catch ( xmlBase::DomException ex )
116 {
117 std::cout << std::endl
118 << "DomException processing badDoubles: " << ex.getMsg() << std::endl
119 << std::endl;
120 }
121
122 if ( argc > 2 )
123 { // attempt to output
124 char* hyphen = "-";
125
126 std::ostream* out;
127
128 if ( *( argv[2] ) == *hyphen ) { out = &std::cout; }
129 else
130 { // try to open file as ostream
131 char* filename = argv[2];
132 out = new std::ofstream( filename );
133 }
134 *out << "Document source: " << std::string( argv[1] ) << std::endl;
135 *out << std::endl << "Straight print of document:" << std::endl;
136 xmlBase::Dom::printElement( docElt, *out );
137 *out << std::endl << std::endl << "Add indentation and line breaks:" << std::endl;
138 xmlBase::Dom::prettyPrintElement( docElt, *out, "" );
139 }
140 }
141 delete parser;
142 return ( 0 );
143}
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
static int getIntAttribute(const DOMNode *elt, std::string attName)
Definition Dom.cxx:297
static void prettyPrintElement(DOMNode *elt, std::ostream &out, std::string prefix)
Definition Dom.cxx:574
static void printElement(DOMNode *elt, std::ostream &out)
Definition Dom.cxx:505
static double getDoubleAttribute(const DOMNode *elt, std::string attName)
Definition Dom.cxx:231
static DOMElement * findFirstChildByName(const DOMElement *parent, const char *const name)
Definition Dom.cxx:58
static unsigned getDoublesAttribute(const DOMNode *elt, std::string attName, std::vector< double > &values, bool clear=true)
Definition Dom.cxx:246
static unsigned getIntsAttribute(const DOMNode *elt, std::string attName, std::vector< int > &values, bool clear=true)
Definition Dom.cxx:310
Exception class for XmlParser, XmlErrorHandler.
DOMDocument * parse(const char *const filename, const std::string &docType=std::string(""))
Parse an xml file, returning document node if successful.
int main()
Definition phokhara.cc:42