Results 1 to 3 of 3

Thread: XML parse

  1. #1
    Join Date
    Apr 2008
    Location
    California
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default XML parse

    Hello,

    I'm trying to read all the values of a particular tag (in this case 'name') with in the xml file.
    below is the actual xml file:

    Qt Code:
    1. <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
    2. <response>
    3. <status>ok</status>
    4. <project_list>
    5. <project>
    6. <id>1911</id>
    7. <url>http://www.snipitron.com/arun/Qt_code</url>
    8. <name>Qt code</name>
    9. </project>
    10. <project>
    11. <id>1902</id>
    12. <url>http://www.snipitron.com/arun/Mac_app</url>
    13. <name>Mac app</name>
    14. </project>
    15. </project_list>
    16. </response>
    To copy to clipboard, switch view to plain text mode 

    And i'm having the Qt code to read the name value as below:

    Qt Code:
    1. if (docElement.tagName() == "response"){
    2. stat = docElement.firstChildElement("status").text();
    3. statMsg = docElement.firstChildElement("message").text();
    4. if (stat == "ok" ){
    5. projNameNode = docElement.elementsByTagName("name");
    6. for (uint j = 0; j < projNameNode.count(); j++)
    7. {
    8. nodeMsg = projNameNode.item(j).toElement().attribute("name");
    9. QMessageBox::critical(this, "Arun", nodeMsg, QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
    10.  
    11. }
    12.  
    13. }
    14. } else
    15. QMessageBox::critical(this, "Arun", "Status: " + stat + " Message: " + statMsg, QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
    16. }
    To copy to clipboard, switch view to plain text mode 

    I'm missing something here and hence i'm getting NULL as value for nodeMsg.

    any help is greatly appreciated.

    thanks,
    Ar

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: XML parse

    You must only iterate xml document ... this here is a rss sample ...

    if you have qt4.4 learn http://doc.trolltech.com/4.4rc1/qxmlquery.html QXmlQuery
    and make a query like a database...


    Qt Code:
    1. typedef QMap<int, QStringList> RssParams;
    2.  
    3. /* read a static rss version 1/2 file and return QMap params of link title description */
    4. RssParams ReadRssFile( QString fileplace , int limiter )
    5. {
    6. RssParams contingent;
    7. QFile xmlfile( fileplace );
    8. QString errorStr, obname, inhalt;
    9. int errorLine, errorColumn, position;
    10. position = 0 -1;
    11. QDomDocument doc("RSS");
    12. if(!xmlfile.open( QIODevice::ReadOnly ) ) {
    13. return contingent;
    14. }
    15. if (!doc.setContent(&xmlfile,true, &errorStr, &errorLine, &errorColumn)) {
    16. xmlfile.close();
    17. return contingent;
    18. }
    19. QDomElement root = doc.documentElement();
    20. QDomElement chanelbase = root.firstChildElement("channel");
    21. QDomElement child = chanelbase.firstChildElement("item");
    22. while (!child.isNull()) {
    23. QString title = child.firstChildElement("title").text();
    24. QString desc = child.firstChildElement("description").text();
    25. QString urirss = child.firstChildElement("link").text();
    26. if (!title.isEmpty() and !desc.isEmpty() and !urirss.isEmpty()) {
    27. position++;
    28. if (position < limiter) {
    29. contingent.insert(position,QStringList() << encodeBase64(urirss) << encodeBase64(title) << encodeBase64(desc));
    30. }
    31. }
    32. child = child.nextSiblingElement("item");
    33. }
    34. xmlfile.close();
    35. return contingent;
    36. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: XML parse

    Attribute is not the same as tag, thus attribute("name") return an empty value, because there is no such attribute. Instead of using attribute(), use QDomElement::text() to retrieve the "value" of each tag.

Similar Threads

  1. Parse QTextBlock from QTextDocument problem
    By patrik08 in forum Qt Programming
    Replies: 0
    Last Post: 5th April 2008, 08:26
  2. Best way to parse a string
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2007, 12:33
  3. 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
  4. How to parse this XML file ?
    By probine in forum Qt Programming
    Replies: 7
    Last Post: 4th April 2006, 09:05
  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.