Results 1 to 3 of 3

Thread: Getting next child element from XML file

  1. #1
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Getting next child element from XML file

    I have a XML file in this format:

    Qt Code:
    1. <heading>
    2. <element1>text</element1>
    3. <element2>text</element2>
    4. <element3>text</element3>
    5. </heading>
    To copy to clipboard, switch view to plain text mode 

    I have code like this:

    Qt Code:
    1. QDomElement element = xmlFile.firstChildElement("heading");
    2.  
    3. QString variable = element.firstChildElement("element1").text();
    To copy to clipboard, switch view to plain text mode 

    How do I get the text of element2? How do I get the text of a specific element? Thanks.

  2. #2
    Join Date
    Nov 2006
    Location
    Saudi Arabia
    Posts
    18
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting next child element from XML file

    use nextSibling();

    here an example from the docs:

    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.  
    12. QDomElement docElem = doc.documentElement();
    13.  
    14. QDomNode n = docElem.firstChild();
    15. while(!n.isNull()) {
    16. QDomElement e = n.toElement();
    17. if(!e.isNull()) {
    18. cout << qPrintable(e.text()) << endl;
    19. }
    20. n = n.nextSibling();
    21. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Getting next child element from XML file


Similar Threads

  1. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  2. Having trouble with direct child nodes using QtXml
    By thelackey3326 in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2008, 21:12
  3. 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
  4. Replies: 1
    Last Post: 10th January 2008, 14:38
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

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
  •  
Qt is a trademark of The Qt Company.