Results 1 to 8 of 8

Thread: How to parse this XML file ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to parse this XML file ?

    This is my simple XML file:

    <question>
    <from>me</me>
    <to>you</to>
    <message>How to I parse this file?</message>
    </question>

    The intention is to get the different words out of the XML elements. The final result should be "me, you, How to I parse this file?".

    Please guide me with a bit of code is possible.

    Thank you.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to parse this XML file ?

    The QDom classes are typically used as follows:

    Qt Code:
    1. QDomDocument doc("mydocument");
    2. QFile file("mydocument.xml");
    3. if (!file.open(QIODevice::ReadOnly))
    4. return;
    5. if (!doc.setContent(&file)) {
    6. file.close();
    7. return;
    8. }
    9. file.close();
    10.  
    11. // print out the element names of all elements that are direct children
    12. // of the outermost element.
    13. QDomElement docElem = doc.documentElement();
    14.  
    15. QDomNode n = docElem.firstChild();
    16. while(!n.isNull()) {
    17. QDomElement e = n.toElement(); // try to convert the node to an element.
    18. if(!e.isNull()) {
    19. cout << e.tagName() << endl; // the node really is an element.
    20. }
    21. n = n.nextSibling();
    22. }
    To copy to clipboard, switch view to plain text mode 
    More information:
    QDomDocument

  3. #3
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to parse this XML file ?

    The example given above prints the name of the elements, not the content of them.

    What is the function call that prints the content of the elements. For example:

    <xml>hello</xml>

    I would like to get the content of the element <xml>, in this case the content should be "hello".

    How ?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to parse this XML file ?

    Did you take a look at the QDomElement documentation?
    J-P Nurmi

  5. #5
    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 parse this XML file ?

    In such a simple case it'll be faster to remove all xml tags

  6. #6
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    23
    Thanks
    3
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to parse this XML file ?

    Use QDomElement::text() instead of QDomElement::tagName() to get the contents of XML instead of getting the tagnames.

    You can alternatively use QXmlContentHandler/QXmlSimpleReader classes to get the contents of the XML.

  7. #7
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to parse this XML file ?

    Hi wysota, I posted a very simple example. In reality the Xml file is much larger.

  8. #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 parse this XML file ?

    It doesn't matter that it is larger. It's important that you need the text in sequencial order and that you don't use attributes.

Similar Threads

  1. Best way to load and parse an HTML file ??
    By tuthmosis in forum Qt Programming
    Replies: 8
    Last Post: 23rd August 2008, 11:06
  2. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 12:19
  5. Parse a pickle file
    By karye in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2006, 17:02

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.