Results 1 to 11 of 11

Thread: Using SAX to parse XML

  1. #1
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Using SAX to parse XML

    hi,
    im trying to parse an xml file of dom parser into sax parser. could u please help me how to do it??

  2. #2
    Join Date
    Jul 2011
    Location
    Pune, India
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read a xml file in qt?

    Quote Originally Posted by sudha View Post
    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??

  3. #3
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to read a xml file in qt?

    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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to read a xml file in qt?

    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

  5. #5
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to read a xml file in qt?

    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
    Last edited by sudha; 13th January 2012 at 08:23.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to read a xml file in qt?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    sudha (13th January 2012)

  8. #7
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to read a xml file in qt?

    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??

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to read a xml file in qt?

    Quote Originally Posted by sudha View Post
    hi........sir,
    i have a doubt..... why we use this xml parsing??
    Are you serious with this question? Are you asking US why YOUR BOSS told you to do it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to read a xml file in qt?

    lol....
    actually im getting many doubts while doing with this so i just want to know where we actually come with this parsing

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to read a xml file in qt?

    Why don't you ask your boss again what you are supposed to do?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to read a xml file in qt?

    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?

  13. The following user says thank you to ChrisW67 for this useful post:

    sudha (20th January 2012)

Similar Threads

  1. parse xml
    By vinayaka in forum Newbie
    Replies: 1
    Last Post: 3rd October 2011, 16:42
  2. how to parse xml with a image using qt?
    By bezlew in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2009, 09:04
  3. parse xml
    By rmagro in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2009, 22:27
  4. XML parse
    By arunredi in forum Newbie
    Replies: 2
    Last Post: 27th April 2008, 01:22
  5. Best way to parse a string
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2007, 12:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.