Logo ROOT   6.08/07
Reference Guide
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 
29 class SaxHandler {
30 public:
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 
45 void SaxHandler::OnEndDocument()
46 {
47  cout << endl;
48 }
49 
50 void 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 
64 void SaxHandler::OnEndElement(const char *name)
65 {
66  cout << "</" << name << ">";
67 }
68 
69 void SaxHandler::OnCharacters(const char *characters)
70 {
71  cout << characters;
72 }
73 
74 void SaxHandler::OnComment(const char *text)
75 {
76  cout << "<!--" << text << "-->";
77 }
78 
79 void SaxHandler::OnWarning(const char *text)
80 {
81  cout << "Warning: " << text << endl;
82 }
83 
84 void SaxHandler::OnError(const char *text)
85 {
86  cerr << "Error: " << text << endl ;
87 }
88 
89 void SaxHandler::OnFatalError(const char *text)
90 {
91  cerr << "FatalError: " << text << endl ;
92 }
93 
94 void SaxHandler::OnCdataBlock(const char *text, Int_t len)
95 {
96  cout << "OnCdataBlock() " << text;
97 }
98 
99 
100 
101 void SAXHandler()
102 {
103  TSAXParser *saxParser = new TSAXParser();
104  SaxHandler *saxHandler = new SaxHandler();
105 
106  saxParser->ConnectToHandler("SaxHandler", saxHandler);
107  TString dir = gROOT->GetTutorialsDir();
108  saxParser->ParseFile(dir+"/xml/saxexample.xml");
109 }
TSAXParser is a subclass of TXMLParser, it is a wraper class to libxml library.
Definition: TSAXParser.h:25
const char * GetValue() const
Definition: TXMLAttr.h:35
#define gROOT
Definition: TROOT.h:364
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
A doubly linked list.
Definition: TList.h:47
TXMLAttribute is the attribute of an Element.
Definition: TXMLAttr.h:20
const char * GetName() const
Returns name of object.
Definition: TXMLAttr.h:33
TText * text
virtual void ConnectToHandler(const char *handlerName, void *handler)
A default TSAXParser to a user-defined Handler connection function.
Definition: TSAXParser.cxx:441
char name[80]
Definition: TGX11.cxx:109
virtual Int_t ParseFile(const char *filename)
It creates the parse context of the xml file, where the xml file name is filename.
Definition: TSAXParser.cxx:236