Logo ROOT  
Reference Guide
SAXHandler.C File Reference

Detailed Description

View in nbviewer Open in SWAN ROOT implementation of a simple SAX Handler.

This handler uses TSAXParser, a SAX Parser using the SAX interface of libxml2. This script will output all elements of the original xml file, if successfully parsed.

To run this program do:

.x SAXHandler.C

Requires: saxexample.xml

<gjob:Helping xmlns:gjob="http://www.gnome.org/some-location">
<gjob:Jobs>
<gjob:Job>
<gjob:Project id="3" time="100" priority="high"></gjob:Project>
<gjob:Application>GBackup</gjob:Application>
<gjob:Category>Development</gjob:Category>
<gjob:Update>
<gjob:Status>Open</gjob:Status>
<gjob:Modified>Mon, 07 Jun 1999 20:27:45 -0400 MET DST</gjob:Modified>
<gjob:Salary>USD 0.00</gjob:Salary>
</gjob:Update>
<gjob:Developers>
<gjob:Developer>
</gjob:Developer>
</gjob:Developers>
<gjob:Contact>
<gjob:Person>Nathan Clemons</gjob:Person>
<gjob:Email>nathan@windsofstorm.net</gjob:Email>
<gjob:Company>
</gjob:Company>
<gjob:Organisation>
</gjob:Organisation>
<gjob:Webpage>
</gjob:Webpage>
<gjob:Snailmail>
</gjob:Snailmail>
<gjob:Phone>
</gjob:Phone>
</gjob:Contact>
<gjob:Requirements>
The program should be released as free software, under the GPL.
</gjob:Requirements>
<gjob:Skills>
</gjob:Skills>
<gjob:Details>
A GNOME based system that will allow a superuser to configure
compressed and uncompressed files and/or file systems to be backed
up with a supported media in the system. This should be able to
perform via find commands generating a list of files that are passed
to tar, dd, cpio, cp, gzip, etc., to be directed to the tape machine
or via operations performed on the filesystem itself. Email
notification and GUI status display very important.
</gjob:Details>
</gjob:Job>
</gjob:Jobs>
</gjob:Helping>
#include <Riostream.h>
#include <TList.h>
#include <TSAXParser.h>
#include <TXMLAttr.h>
class SaxHandler {
public:
SaxHandler() { }
void OnStartDocument() { }
void OnEndDocument();
void OnStartElement(const char*, const TList*);
void OnEndElement(const char*);
void OnCharacters(const char*);
void OnComment(const char*);
void OnWarning(const char*);
void OnError(const char*);
void OnFatalError(const char*);
void OnCdataBlock(const char*, Int_t);
};
void SaxHandler::OnEndDocument()
{
cout << endl;
}
void SaxHandler::OnStartElement(const char *name, const TList *attributes)
{
cout << "<" << name;
TXMLAttr *attr;
TIter next(attributes);
while ((attr = (TXMLAttr*) next())) {
cout << " " << attr->GetName() << "=\"" << attr->GetValue() << "\"";
}
cout << ">";
}
void SaxHandler::OnEndElement(const char *name)
{
cout << "</" << name << ">";
}
void SaxHandler::OnCharacters(const char *characters)
{
cout << characters;
}
void SaxHandler::OnComment(const char *text)
{
cout << "<!--" << text << "-->";
}
void SaxHandler::OnWarning(const char *text)
{
cout << "Warning: " << text << endl;
}
void SaxHandler::OnError(const char *text)
{
cerr << "Error: " << text << endl ;
}
void SaxHandler::OnFatalError(const char *text)
{
cerr << "FatalError: " << text << endl ;
}
void SaxHandler::OnCdataBlock(const char *text, Int_t len)
{
cout << "OnCdataBlock() " << text;
}
void SAXHandler()
{
TSAXParser *saxParser = new TSAXParser();
SaxHandler *saxHandler = new SaxHandler();
saxParser->ConnectToHandler("SaxHandler", saxHandler);
TString dir = gROOT->GetTutorialDir();
saxParser->ParseFile(dir+"/xml/saxexample.xml");
}
Author
Sergey Linev

Definition in file SAXHandler.C.

TSAXParser.h
TXMLAttr
Definition: TXMLAttr.h:18
TXMLAttr::GetValue
const char * GetValue() const
Definition: TXMLAttr.h:39
Int_t
int Int_t
Definition: RtypesCore.h:45
TList.h
TString
Definition: TString.h:136
TSAXParser::ConnectToHandler
virtual void ConnectToHandler(const char *handlerName, void *handler)
A default TSAXParser to a user-defined Handler connection function.
Definition: TSAXParser.cxx:441
text
TText * text
Definition: entrylist_figure1.C:10
TSAXParser::ParseFile
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
TXMLAttr.h
TXMLAttr::GetName
const char * GetName() const
Returns name of object.
Definition: TXMLAttr.h:37
TSAXParser
Definition: TSAXParser.h:23
name
char name[80]
Definition: TGX11.cxx:110
TIter
Definition: TCollection.h:233
Riostream.h
TList
Definition: TList.h:44
gROOT
#define gROOT
Definition: TROOT.h:406