ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SAXHandler.C
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // ROOT implementation of a simple SAX Handler.
4 //
5 // This handler uses TSAXParser, a SAX Parser using the SAX interface
6 // of libxml2. This script will output all elements of the original xml
7 // file, if sucessfully parsed.
8 //
9 // To run this program do:
10 // .x SAXHandler.C
11 //
12 // Requires: saxexample.xml
13 //
14 //////////////////////////////////////////////////////////////////////////////
15 
16 #include <Riostream.h>
17 #include <TList.h>
18 #include <TSAXParser.h>
19 #include <TXMLAttr.h>
20 
21 
22 class SaxHandler {
23 public:
24  SaxHandler() { }
25 
26  void OnStartDocument() { }
27  void OnEndDocument();
28  void OnStartElement(const char*, const TList*);
29  void OnEndElement(const char*);
30  void OnCharacters(const char*);
31  void OnComment(const char*);
32  void OnWarning(const char*);
33  void OnError(const char*);
34  void OnFatalError(const char*);
35  void OnCdataBlock(const char*, Int_t);
36 };
37 
38 void SaxHandler::OnEndDocument()
39 {
40  cout << endl;
41 }
42 
43 void SaxHandler::OnStartElement(const char *name, const TList *attributes)
44 {
45  cout << "<" << name;
46 
47  TXMLAttr *attr;
48 
49  TIter next(attributes);
50  while ((attr = (TXMLAttr*) next())) {
51  cout << " " << attr->GetName() << "=\"" << attr->GetValue() << "\"";
52  }
53 
54  cout << ">";
55 }
56 
57 void SaxHandler::OnEndElement(const char *name)
58 {
59  cout << "</" << name << ">";
60 }
61 
62 void SaxHandler::OnCharacters(const char *characters)
63 {
64  cout << characters;
65 }
66 
67 void SaxHandler::OnComment(const char *text)
68 {
69  cout << "<!--" << text << "-->";
70 }
71 
72 void SaxHandler::OnWarning(const char *text)
73 {
74  cout << "Warning: " << text << endl;
75 }
76 
77 void SaxHandler::OnError(const char *text)
78 {
79  cerr << "Error: " << text << endl ;
80 }
81 
82 void SaxHandler::OnFatalError(const char *text)
83 {
84  cerr << "FatalError: " << text << endl ;
85 }
86 
87 void SaxHandler::OnCdataBlock(const char *text, Int_t len)
88 {
89  cout << "OnCdataBlock() " << text;
90 }
91 
92 
93 
94 void SAXHandler()
95 {
96  TSAXParser *saxParser = new TSAXParser();
97  SaxHandler *saxHandler = new SaxHandler();
98 
99  saxParser->ConnectToHandler("SaxHandler", saxHandler);
100  TString dir = gSystem->DirName(__FILE__);
101  saxParser->ParseFile(dir+"/saxexample.xml");
102 }
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
TSAXParser is a subclass of TXMLParser, it is a wraper class to libxml library.
Definition: TSAXParser.h:25
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:980
A doubly linked list.
Definition: TList.h:47
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
TXMLAttribute is the attribute of an Element.
Definition: TXMLAttr.h:20
const char * GetValue() const
Definition: TXMLAttr.h:35
TText * text
void dir(char *path=0)
Definition: rootalias.C:30
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual void ConnectToHandler(const char *handlerName, void *handler)
A default TSAXParser to a user-defined Handler connection function.
Definition: TSAXParser.cxx:441
void SAXHandler()
Definition: SAXHandler.C:94
const char * GetName() const
Returns name of object.
Definition: TXMLAttr.h:33
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