CGEM BOSS 6.6.5.g
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/xmlBase/xmlBase-00-00-03/src/main.cxx File Reference
#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[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Test program for xmlBase facility. Parse xml file and optionally write it out to a stream. 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 Calibration/xmlBase/xmlBase-00-00-03/src/main.cxx.

21 {
22 XERCES_CPP_NAMESPACE_USE
23
24 std::string infile;
25 if (argc < 2) {
26 infile=std::string("$(XMLBASEROOT)/xml/test.xml");
27 }
28 else {
29 infile = std::string(argv[1]);
30 }
31
33
34 xmlBase::XmlParser* parser = new xmlBase::XmlParser(true);
35
36 DOMDocument* doc = 0;
37 try {
38 doc = parser->parse(infile.c_str());
39 }
40 catch (xmlBase::ParseException ex) {
41 std::cout << "caught exception with message " << std::endl;
42 std::cout << ex.getMsg() << std::endl;
43 delete parser;
44 return 0;
45 }
46
47 if (doc != 0) { // successful
48 std::cout << "Document successfully parsed" << std::endl;
49
50 // look up some attributes
51 DOMElement* docElt = doc->getDocumentElement();
52 DOMElement* attElt =
53 xmlBase::Dom::findFirstChildByName(docElt, "ChildWithAttributes");
54 double doubleVal;
55 int intVal;
56
57 try {
58 intVal = xmlBase::Dom::getIntAttribute(attElt, "goodInt");
59 std::cout << "goodInt value was " << intVal << std::endl << std::endl;
60 }
61 catch (xmlBase::DomException ex) {
62 std::cout << std::endl << "DomException: " << ex.getMsg()
63 << std::endl << std::endl;
64 }
65
66 try {
67 std::vector<int> ints;
68 unsigned nInts = xmlBase::Dom::getIntsAttribute(attElt, "goodInts", ints);
69 std::cout << "Found " << nInts << " goodInts: " << std::endl;
70 for (unsigned iInt=0; iInt < nInts; iInt++) {
71 std::cout << ints[iInt] << " ";
72 }
73 std::cout << std::endl << std::endl;
74 }
75 catch (xmlBase::DomException ex) {
76 std::cout << std::endl << "DomException processing goodInts: "
77 << ex.getMsg() << std::endl << std::endl;
78 }
79
80 try {
81 std::vector<double> doubles;
82 unsigned nD = xmlBase::Dom::getDoublesAttribute(attElt, "goodDoubles",
83 doubles);
84 std::cout << "Found " << nD << " goodDoubles: " << std::endl;
85 for (unsigned iD=0; iD < nD; iD++) {
86 std::cout << doubles[iD] << " ";
87 }
88 std::cout << std::endl << std::endl;
89 }
90 catch (xmlBase::DomException ex) {
91 std::cout << std::endl << "DomException processing goodDoubles: "
92 << ex.getMsg() << std::endl << std::endl;
93 }
94
95
96
97 try {
98 intVal = xmlBase::Dom::getIntAttribute(attElt, "badInt");
99 std::cout << "badInt value was " << intVal << std::endl << std::endl;
100 }
101 catch (xmlBase::DomException ex) {
102 std::cout << std::endl << "DomException: " << ex.getMsg()
103 << std::endl << std::endl;
104 }
105
106 try {
107 doubleVal = xmlBase::Dom::getDoubleAttribute(attElt, "goodDouble");
108 std::cout << "goodDouble value was " << doubleVal
109 << std::endl << std::endl;
110 }
111 catch (xmlBase::DomException ex) {
112 std::cout << std::endl << "DomException: " << ex.getMsg()
113 << std::endl << std::endl;
114 }
115
116 try {
117 doubleVal = xmlBase::Dom::getDoubleAttribute(attElt, "badDouble");
118 std::cout << std::endl << "badDouble value was " << doubleVal
119 << std::endl << std::endl;
120 }
121 catch (xmlBase::DomException ex) {
122 std::cout << std::endl << "DomException: " << ex.getMsg()
123 << std::endl << std::endl;
124 }
125
126
127 try {
128 std::vector<double> doubles;
129 unsigned nD = xmlBase::Dom::getDoublesAttribute(attElt, "badDoubles",
130 doubles);
131 std::cout << "Found " << nD << " badDoubles: " << std::endl;
132 for (unsigned iD=0; iD < nD; iD++) {
133 std::cout << doubles[iD] << " ";
134 }
135 std::cout << std::endl << std::endl;
136 }
137 catch (xmlBase::DomException ex) {
138 std::cout << std::endl << "DomException processing badDoubles: "
139 << ex.getMsg()
140 << std::endl << std::endl;
141 }
142
143 if (argc > 2) { // attempt to output
144 char *hyphen = "-";
145
146 std::ostream* out;
147
148 if (*(argv[2]) == *hyphen) {
149 out = &std::cout;
150 }
151 else { // try to open file as ostream
152 char *filename = argv[2];
153 out = new std::ofstream(filename);
154 }
155 *out << "Document source: " << std::string(argv[1]) << std::endl;
156 *out << std::endl << "Straight print of document:" << std::endl;
157 xmlBase::Dom::printElement(docElt, *out);
158 *out << std::endl << std::endl << "Add indentation and line breaks:"
159 << std::endl;
160 xmlBase::Dom::prettyPrintElement(docElt, *out, "");
161 }
162 }
163 delete parser;
164 return(0);
165}
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
Base exception class for Dom.
Definition: Dom.h:29
virtual std::string getMsg()
Definition: Dom.h:34
static int getIntAttribute(const DOMNode *elt, std::string attName)
Definition: Dom.cxx:326
static void prettyPrintElement(DOMNode *elt, std::ostream &out, std::string prefix)
Definition: Dom.cxx:644
static void printElement(DOMNode *elt, std::ostream &out)
Definition: Dom.cxx:570
static double getDoubleAttribute(const DOMNode *elt, std::string attName)
Definition: Dom.cxx:263
static DOMElement * findFirstChildByName(const DOMElement *parent, const char *const name)
Definition: Dom.cxx:60
static unsigned getDoublesAttribute(const DOMNode *elt, std::string attName, std::vector< double > &values, bool clear=true)
Definition: Dom.cxx:279
static unsigned getIntsAttribute(const DOMNode *elt, std::string attName, std::vector< int > &values, bool clear=true)
Definition: Dom.cxx:342
Exception class for XmlParser, XmlErrorHandler.
virtual std::string getMsg()
DOMDocument * parse(const char *const filename, const std::string &docType=std::string(""))
Parse an xml file, returning document node if successful.
Definition: XmlParser.cxx:108