BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
entity_test.cxx File Reference
#include <xercesc/dom/DOM.hpp>
#include <xercesc/framework/LocalFileInputSource.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/sax/ErrorHandler.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/XMLUni.hpp>
#include <iostream>
#include <string>

Go to the source code of this file.

Classes

class  XmlErrorHandler

Functions

void Process (DOMDocument *doc, char *eltName)
int main (int argc, char *argv[])

Function Documentation

◆ main()

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

Definition at line 40 of file entity_test.cxx.

40 {
41
42 std::string infile;
43 if ( argc < 2 )
44 { infile = std::string( "/u/ey/jrb/dev500/vanilla/xmlBase/v5r1/xml/simpleDoc.xml" ); }
45 else { infile = std::string( argv[1] ); }
46
47 try
48 { XMLPlatformUtils::Initialize(); } catch ( const XMLException& toCatch )
49 { // may want to redirect in Gaudi environment
50 char* charMsg = XMLString::transcode( toCatch.getMessage() );
51 std::string msg = std::string( charMsg );
52 XMLString::release( &charMsg );
53 // std::string msg(Dom::transToChar(toCatch.getMessage()));
54 std::cerr << "Error during Xerces-c Initialization.\n"
55 << " Exception message:" << msg << std::endl;
56 return 0;
57 }
58
59 const char* inChars = infile.c_str();
60 XMLCh* xmlchPath = XMLString::transcode( inChars );
61 XercesDOMParser* parser = new XercesDOMParser();
62
63 XmlErrorHandler* errorHandler = new XmlErrorHandler();
64 parser->setErrorHandler( errorHandler );
65
66 // The following made no difference in the output
67 // parser->useScanner(XMLUni::fgDGXMLScanner);
68 // According to documentation we shouldn't need this, but
69 // can't hurt
70 parser->setValidationScheme( AbstractDOMParser::Val_Auto );
71
72 std::cout << "create entity reference flag has default (true) value" << std::endl;
73 parser->parse( xmlchPath );
74 DOMDocument* doc = parser->getDocument();
75 std::cout << "Document successfully parsed" << std::endl;
76 std::cout << "processing local elements.. " << std::endl;
77 Process( doc, "const" );
78 std::cout << std::endl << "processing external elements.. " << std::endl;
79 Process( doc, "extConst" );
80
81 parser->reset();
82 parser->setCreateEntityReferenceNodes( false );
83
84 std::cout << std::endl
85 << std::endl
86 << "create entity reference flag has value false" << std::endl;
87 parser->parse( xmlchPath );
88 DOMDocument* expandDoc = parser->getDocument();
89 std::cout << "Document successfully parsed" << std::endl;
90 std::cout << "processing local elements.. " << std::endl;
91 Process( expandDoc, "const" );
92 std::cout << "processing external elements.. " << std::endl;
93 Process( expandDoc, "extConst" );
94}
void Process(DOMDocument *doc, char *eltName)

◆ Process()

void Process ( DOMDocument * doc,
char * eltName )

Definition at line 96 of file entity_test.cxx.

96 {
97 XMLCh* xmlchEltName = XMLString::transcode( eltName );
98 XMLCh* xmlchName = XMLString::transcode( "name" );
99 XMLCh* xmlchValue = XMLString::transcode( "value" );
100 XMLCh* xmlchModified = XMLString::transcode( "modified" );
101 if ( !doc )
102 {
103 std::cerr << "invalid document " << std::endl;
104 return;
105 }
106
107 // Find all elements with supplied tag name
108 DOMNodeList* constList = doc->getElementsByTagName( xmlchEltName );
109 unsigned int nElt = constList->getLength();
110 for ( unsigned int iElt = 0; iElt < nElt; iElt++ )
111 {
112 try
113 {
114 bool mismatch = false;
115 DOMNode* item = constList->item( iElt );
116 DOMElement* itemElt = dynamic_cast<DOMElement*>( item );
117 DOMElement* byIdElt;
118 std::cout << std::endl
119 << eltName << " #" << iElt << " Address as node: " << item
120 << " and as element: " << itemElt << std::endl;
121 const XMLCh* xmlchNamevalue = itemElt->getAttribute( xmlchName );
122 if ( XMLString::stringLen( xmlchNamevalue ) > 0 )
123 {
124 char* namevalue = XMLString::transcode( xmlchNamevalue );
125 std::cout << "element has name " << namevalue << std::endl;
126 byIdElt = doc->getElementById( xmlchNamevalue );
127 std::cout << "Address from getElementById: " << byIdElt << std::endl;
128 if ( byIdElt != itemElt )
129 {
130 mismatch = true;
131 std::cout << "**** Address mismatch " << std::endl << std::endl;
132 }
133 XMLString::release( &namevalue );
134 }
135 std::cout << "Modifying value attribute using DOM_Element address" << std::endl;
136 itemElt->setAttribute( xmlchValue, xmlchModified );
137 if ( mismatch )
138 {
139 std::cout << "Modifying value attribute using looked-up address" << std::endl;
140 byIdElt->setAttribute( xmlchValue, xmlchModified );
141 }
142 } catch ( DOMException ex )
143 {
144 int code = ex.code;
145 std::cout << "***** Processing failed for element #" << iElt
146 << " with DOMException, code = " << code << std::endl
147 << std::endl;
148 }
149 }
150}

Referenced by main().