Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
SAXHandler.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_xml
3/// \notebook -nodraw
4/// ROOT implementation of a simple SAX Handler.
5///
6/// This handler uses TSAXParser, a SAX Parser using the SAX interface
7/// of libxml2. This script will output all elements of the original xml
8/// file, if successfully parsed.
9///
10/// To run this program do:
11/// ~~~{.cpp}
12/// .x SAXHandler.C
13/// ~~~
14///
15/// Requires: `saxexample.xml`
16///
17///
18/// \macro_output
19/// \macro_code
20///
21/// \author Sergey Linev
22
23#include <Riostream.h>
24#include <TList.h>
25#include <TSAXParser.h>
26#include <TXMLAttr.h>
27
28
29class SaxHandler {
30public:
31 SaxHandler() { }
32
33 void OnStartDocument() { }
34 void OnEndDocument();
35 void OnStartElement(const char*, const TList*);
36 void OnEndElement(const char*);
37 void OnCharacters(const char*);
38 void OnComment(const char*);
39 void OnWarning(const char*);
40 void OnError(const char*);
41 void OnFatalError(const char*);
42 void OnCdataBlock(const char*, Int_t);
43};
44
45void SaxHandler::OnEndDocument()
46{
47 cout << endl;
48}
49
50void SaxHandler::OnStartElement(const char *name, const TList *attributes)
51{
52 cout << "<" << name;
53
54 TXMLAttr *attr;
55
56 TIter next(attributes);
57 while ((attr = (TXMLAttr*) next())) {
58 cout << " " << attr->GetName() << "=\"" << attr->GetValue() << "\"";
59 }
60
61 cout << ">";
62}
63
64void SaxHandler::OnEndElement(const char *name)
65{
66 cout << "</" << name << ">";
67}
68
69void SaxHandler::OnCharacters(const char *characters)
70{
71 cout << characters;
72}
73
74void SaxHandler::OnComment(const char *text)
75{
76 cout << "<!--" << text << "-->";
77}
78
79void SaxHandler::OnWarning(const char *text)
80{
81 cout << "Warning: " << text << endl;
82}
83
84void SaxHandler::OnError(const char *text)
85{
86 cerr << "Error: " << text << endl ;
87}
88
89void SaxHandler::OnFatalError(const char *text)
90{
91 cerr << "FatalError: " << text << endl ;
92}
93
94void SaxHandler::OnCdataBlock(const char *text, Int_t len)
95{
96 cout << "OnCdataBlock() " << text;
97}
98
99
100
101void SAXHandler()
102{
103 TSAXParser *saxParser = new TSAXParser();
104 SaxHandler *saxHandler = new SaxHandler();
105
106 saxParser->ConnectToHandler("SaxHandler", saxHandler);
107 TString dir = gROOT->GetTutorialDir();
108 saxParser->ParseFile(dir+"/xml/saxexample.xml");
109}
int Int_t
Definition RtypesCore.h:45
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:406
A doubly linked list.
Definition TList.h:44
TSAXParser is a subclass of TXMLParser, it is a wraper class to libxml library.
Definition TSAXParser.h:23
virtual Int_t ParseFile(const char *filename)
It creates the parse context of the xml file, where the xml file name is filename.
virtual void ConnectToHandler(const char *handlerName, void *handler)
A default TSAXParser to a user-defined Handler connection function.
Basic string class.
Definition TString.h:136
TXMLAttribute is the attribute of an Element.
Definition TXMLAttr.h:18
const char * GetValue() const
Definition TXMLAttr.h:33
const char * GetName() const
Returns name of object.
Definition TXMLAttr.h:31
TText * text