gatb.core-API-1.4.2
XmlReader Class Reference

Simple implementation of an XML (SAX) parser. More...

#include <XmlReader.hpp>

Inheritance diagram for XmlReader:
Inheritance graph

Public Member Functions

 XmlReader (std::istream &is)
 
virtual ~XmlReader ()
 
void read ()
 
- Public Member Functions inherited from Subject
 Subject ()
 
 Subject (const InterfaceId &interface)
 
virtual ~Subject ()
 
InterfaceId getInterface ()
 
void addObserver (IObserver *observer)
 
void removeObserver (IObserver *observer)
 
void notify (EventInfo *event)
 
- Public Member Functions inherited from ISubject
virtual ~ISubject ()
 

Detailed Description

Simple implementation of an XML (SAX) parser.

This implementation considers that the reader is mainly a Subject, and therefore sends notification to potential listeners as its parsing goes on.

Any attached observer is likely to receive instances of subclasses of XMLEvent, and has to do something in reaction.

// We create some XML listener class
class XmlListener : public IObserver
{
public:
void update (EventInfo* evt, ISubject* subject)
{
XmlTagOpenEvent* open = dynamic_cast<XmlTagOpenEvent*> (evt);
if (open) { cout << "open '" << open->_name << "'" << endl; return; }
XmlTagCloseEvent* close = dynamic_cast<XmlTagCloseEvent*> (evt);
if (close) { cout << "close '" << close->_name << "'" << endl; return; }
XmlTagTextEvent* text = dynamic_cast<XmlTagTextEvent*> (evt);
if (text) { cout << "text '" << text->_txt << "'" << endl; return; }
XmlTagAttributeEvent* attribute = dynamic_cast<XmlTagAttributeEvent*> (evt);
if (attribute) { cout << "attribute: name='" << attribute->_name << "' value='" << attribute->_value << "'" << endl; return; }
}
};
void foo ()
{
// We define some input stream holding an XML string
stringstream is (stringstream::in | stringstream::out);
is << "<properties><progression><exec_percentage>100</exec_percentage><nb_alignments>4257</nb_alignments></progression>properties>";
// We create a reader with our input stream.
XmlReader reader (is);
// We instantiate one observer
IObserver* listener = new XmlListener();
// We attach the listener to the reader
reader.addObserver (listener);
// We read the XML stream
reader.read();
// We detach the listener from the reader.
reader.removeObserver (listener);
}
See also
XmlTagOpenEvent
XmlTagCloseEvent
XmlTagTextEvent
XmlTagAttributeEvent

Constructor & Destructor Documentation

XmlReader ( std::istream &  is)

Constructor.

Parameters
[in]is: the input stream containing the XML stream.
~XmlReader ( )
virtual

Destructor.

Member Function Documentation

void read ( )

Parse the input stream and possibly sends notifications to potential observers.


The documentation for this class was generated from the following files: