Results 1 to 8 of 8

Thread: Parsing XML file

  1. #1
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Parsing XML file

    Hi!
    I need to parse this xml file of this structure:

    <tcdescription>
    <platform type="common">
    <id>wdt_0003</id>
    <owner>TB</owner>
    <requirement>random_requirement</requirement>
    <type id="module"></type>
    <style type="automatic"></style>
    <module>WDT</module>
    <level id="0"></level>
    <effort unit="h">1</effort>
    <startertimeout>100000</startertimeout>
    <description>
    bla bla
    </description>
    </platform>

    <platform type="vgca" os="Windows">
    <activated status="true"></activated>
    <timeout>bla bla</timeout>
    <trace use_automatic="yes"></trace>
    <debug use_automatic="yes"></debug>

    <compilation type="debug" default="yes">
    <executable>bla bla</executable>
    </compilation>
    <compilation type="release">
    <executable>bla bla</executable>
    </compilation>
    </platform>

    <platform type="vgcb" os="Windows">
    <activated status="true"></activated>
    <timeout>bla bla</timeout>
    <trace use_automatic="yes"></trace>
    <debug use_automatic="yes"></debug>

    <compilation type="debug" default="yes">
    <executable>bla bla</executable>
    </compilation>
    <compilation type="release">
    <executable>bla bla</executable>
    </compilation>
    </platform>
    </tcdescription>
    --------------------------------------------------------

    I use this code
    Qt Code:
    1. QDomNode platform = domDocument.firstChild();
    2. platform=platform.firstChild();
    3. while (!platform.isNull())
    4. {
    5. QDomElement e = platform.toElement();
    6. cout<<e.tagName().toStdString();
    7. platform=platform.nextSibling();
    8. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Parsing XML file

    Hmm...nice of you to show us your code...but what is your point exactly? ;-)

    (Could it be that you forgot a part of your message?)

  3. #3
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parsing XML file

    You are right, I messed up my post The point is that my code never enters the while loop. The condition is never fullfiled, and I don't get it why. What am I doing wrong?

  4. #4
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing XML file

    Quote Originally Posted by Djony View Post
    What am I doing wrong?
    My guess would be that you should try documentElement instead of firstChild on the domDocument. (And perhaps it would be easier to stick with the firstChildElement and nextSiblingElement functions, instead of working with DomNodes the whole time...)

    (This is a shot from the hip...no guarantees on correctness ;-)
    Last edited by camel; 8th January 2007 at 13:16. Reason: updated contents

  5. #5
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parsing XML file

    You are great hip shooter Yes, that solved it, thanx. Do you maybe know how could I extract from this XML line:

    <platform type="common">
    --------------------------------------

    string "type ="common" ?

  6. #6
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing XML file

    The docs are your friend ;-)


    Try attributeNode

    i.e.
    Qt Code:
    1. QDomAttr attr = element.attributeNode(QLatin1String("type"));
    To copy to clipboard, switch view to plain text mode 

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

    Djony (8th January 2007)

  8. #7
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parsing XML file

    Thanks for your replies. That did it. I have another problem. When I am browsing through my tags (id,owner,requirement,module,... look at the structure of xml file in above post) I use this line
    Qt Code:
    1. platformDetail=platformDetail.nextSiblingElement(platformDetail.tagName());
    2. //platformDetail is QDomElement class
    To copy to clipboard, switch view to plain text mode 
    And I can't get any tags. Do you have any idea what could be problem? Similar line worked fine for platform tags...

  9. #8
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing XML file

    Quote Originally Posted by Djony View Post
    Do you have any idea what could be problem?
    Once again. Check the docs:
    Returns the next sibilng element with tag name tagName if tagName is non-empty; otherwise returns any next sibling element.
    What does that tell you? You try to get the next sibling element with the same name as the current element. Is that what you want?

    Please take the time to read through the documentation, perhaps starting from here, as the qdomdocument page is a good entry point.

Similar Threads

  1. Draging a non-existing file to the Windows Desktop
    By klaus1111 in forum Qt Programming
    Replies: 13
    Last Post: 20th September 2007, 11:47
  2. How To Extract A File
    By deekayt in forum General Programming
    Replies: 7
    Last Post: 5th December 2006, 18:27
  3. Config file parsing
    By Vash5556 in forum Qt Programming
    Replies: 2
    Last Post: 10th September 2006, 23:11
  4. SQLite-DB in a qrc file
    By Lykurg in forum Qt Programming
    Replies: 5
    Last Post: 31st July 2006, 19:24
  5. Accessing DTD in an XML file via QtXml module?
    By jorma in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2006, 18:09

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.