PDA

View Full Version : Using SAX to parse XML



sudha
12th January 2012, 05:18
hi,
im trying to parse an xml file of dom parser into sax parser. could u please help me how to do it??

ommharidaas
12th January 2012, 06:31
hi,
im trying to parse an xml file of dom parser into sax parser. could u please help me how to do it??

what do u mean when you say "xml file of dom parser into sax parser"???
What exactly you trying to achieve??

sudha
12th January 2012, 08:48
I mean I am trying to parse a xml file using sax parsing methods instead of conventional dom parsing methods. so i have to replace all the methods of dom(i.e QDomElement,QDomDocument) to sax for parsing an xml file.please if u not get my point let me know

ChrisW67
13th January 2012, 02:17
The DOM and SAX are completely different approaches to handling an XML document. There is no simple correspondence between code using the different approaches, i.e. you cannot simply change a few class names and switch between them.

For information on how to use Qt to read XML as a stream (SAX) read: XML Streaming and look at the SAX Bookmarks Example

sudha
13th January 2012, 05:19
an example of xml parsing using SAX but the code is in QDomDocument so i replaced all the QDomDocument with SAX functions so i have done with the constructor part like:

Actual code for constructor:

XMLparser::XMLparser( const QDomDocument & document ) :
filename ( "" ),
isvalid (false),
lasterrorstring("")
{

domdocument = document;
mroot = mdomdocument.documentElement ();

if( root.isNull () )
{
cout<< "Dom document contained no root node;
return;
}

currentnode = root;

isvalid = true;
}

Reimplemnted constructor:

XMLparser::XMLparser(const QXmlInputSource &inputSource) :
isValid (false),
lastErrorString(""),
xmlInputSource(inputSource)
{

xmlFile.setFileName (xmlFileName);

xmlInputSource=QXmlInputSource(&xmlFile);

xmlSimpleReader.setContentHandler(&xmlHandler);

bool parseSource = xmlSimpleReader.parse(xmlInputSource);

if(parseSource)
std::cout<<"The file can be parsed";
else
std::cout<<"The file cannot be parsed";


}


another method:

QString XMLparser::getnode_name ( void ) const
{

if( ! isvalid )
{
return ( "" );
}

return ( currentnode.toElement () ).tagName ();
}

The above Reimplemented constructor is modified with SAX function and it is working but my doubt is that SAX is an event based and DOM is a tree based so how can we do this is there any alternative methods available for doing it like "get root node","current node","next sibling".... nodes in SAX. please help me out :(

wysota
13th January 2012, 06:26
As Chris told you, the philisophy behind SAX and DOM is completely different. With SAX you can't "get root node" or "next sibling". It works the other way round -- the framework calls one of your methods and says "ok, and now here is a tag for you -- handle it". I would really advise AGAINST using pure SAX approach in favour of using QXmlStreamReader which is much easier to handle. It has all the benefits of SAX approach but none of its drawbacks.

sudha
19th January 2012, 09:59
hi........sir,
i have a doubt..... why we use this xml parsing?? ,what is the need of this parsing?? and can it be used in real time applications??

wysota
19th January 2012, 10:03
hi........sir,
i have a doubt..... why we use this xml parsing??
Are you serious with this question? :D Are you asking US why YOUR BOSS told you to do it?

sudha
19th January 2012, 10:19
lol.... :D
actually im getting many doubts while doing with this so i just want to know where we actually come with this parsing

wysota
19th January 2012, 11:31
Why don't you ask your boss again what you are supposed to do?

ChrisW67
19th January 2012, 23:12
The DOM approach loads the entire document into a defined in-memory structure that you can later randomly access and manipulate using defined functions. For files like settings or XML representations of an editable document this is often a good choice.

The SAX approach asks you program to react to a parts of a stream of XML as it arrives, either by reading from a file or across a network etc., and do something there and then. For constructing your own in-memory data structures or performing some immediate action in response to the XML in a single pass SAX is a good choice.

Efficiency generally says that you do not rewrite something that works, i.e. your DOM solution, unless there is a good reason. What are you trying to achieve?