Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
SAXHandler.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_xml
3///
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/// \macro_code
18///
19/// \author Sergey Linev
20
21#include <Riostream.h>
22#include <TList.h>
23#include <TSAXParser.h>
24#include <TXMLAttr.h>
25
26
27class SaxHandler {
28public:
29 SaxHandler() { }
30
31 void OnStartDocument() { }
32 void OnEndDocument();
33 void OnStartElement(const char*, const TList*);
34 void OnEndElement(const char*);
35 void OnCharacters(const char*);
36 void OnComment(const char*);
37 void OnWarning(const char*);
38 void OnError(const char*);
39 void OnFatalError(const char*);
40 void OnCdataBlock(const char*, Int_t);
41};
42
43void SaxHandler::OnEndDocument()
44{
45 cout << endl;
46}
47
48void SaxHandler::OnStartElement(const char *name, const TList *attributes)
49{
50 cout << "<" << name;
51
53
54 TIter next(attributes);
55 while ((attr = (TXMLAttr*) next())) {
56 cout << " " << attr->GetName() << "=\"" << attr->GetValue() << "\"";
57 }
58
59 cout << ">";
60}
61
62void SaxHandler::OnEndElement(const char *name)
63{
64 cout << "</" << name << ">";
65}
66
67void SaxHandler::OnCharacters(const char *characters)
68{
69 cout << characters;
70}
71
72void SaxHandler::OnComment(const char *text)
73{
74 cout << "<!--" << text << "-->";
75}
76
77void SaxHandler::OnWarning(const char *text)
78{
79 cout << "Warning: " << text << endl;
80}
81
82void SaxHandler::OnError(const char *text)
83{
84 cerr << "Error: " << text << endl ;
85}
86
87void SaxHandler::OnFatalError(const char *text)
88{
89 cerr << "FatalError: " << text << endl ;
90}
91
92void SaxHandler::OnCdataBlock(const char *text, Int_t len)
93{
94 cout << "OnCdataBlock() " << text;
95}
96
97
98
99void SAXHandler()
100{
101 TSAXParser *saxParser = new TSAXParser();
102 SaxHandler *saxHandler = new SaxHandler();
103
104 saxParser->ConnectToHandler("SaxHandler", saxHandler);
105 TString dir = gROOT->GetTutorialDir();
106 saxParser->ParseFile(dir+"/xml/saxexample.xml");
107}
int Int_t
Definition RtypesCore.h:45
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
Option_t Option_t TPoint TPoint const char text
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:406
A doubly linked list.
Definition TList.h:38
TSAXParser is a subclass of TXMLParser, it is a wraper class to libxml library.
Definition TSAXParser.h:23
virtual void ConnectToHandler(const char *handlerName, void *handler)
A default TSAXParser to a user-defined Handler connection function.
Int_t ParseFile(const char *filename) override
It creates the parse context of the xml file, where the xml file name is filename.
Basic string class.
Definition TString.h:139
TXMLAttribute is the attribute of an Element.
Definition TXMLAttr.h:18