Results 1 to 3 of 3

Thread: QXmlStreamReader not reading text within tags

  1. #1
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default QXmlStreamReader not reading text within tags

    I am trying to read the text in between xml tags using a simple QXmlStreamReader

    i have filled a QXmlStreamReader called sectionReader with xml from this link:http://www.waseet.net/apps/iphone/xm...ories-file.xml

    this code shows all start elements normally:
    void MainWindow:arseXml2()
    {
    while (!sectionReader.atEnd())
    {
    sectionReader.readNext();
    if(sectionReader.isStartElement())
    {
    ui->textEdit->append(sectionReader.name().toString());
    }
    }
    }

    whereas this code gives whitespaces:
    void MainWindow:arseXml2()
    {
    while (!sectionReader.atEnd())
    {
    sectionReader.readNext();
    if(sectionReader.isStartElement())
    {
    ui->textEdit->append(sectionReader.readElementText());
    }
    }
    }

    can anyone indicate what is going wrong? why aren't i reading any text in between the start and end tags?
    I can verify that the xml has text in between the tags. here is the link if anyone wants to check: http://www.waseet.net/apps/iphone/xm...ories-file.xml

    i have attached my .h and .cpp files along with the post
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QXmlStreamReader not reading text within tags

    I haven't looked in detail at your code, but the XML is unusual in that everything inside the elements is contained in CDATA markers. This may exclude it from being considered as text by QXmlStreamReader because it could be any arbitrary binary.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QXmlStreamReader not reading text within tags

    On closer inspection:
    • Your code starts and encounters the <root> element
    • isStartElement() is true
    • You read the entire content of the <root> element to extract any immediate child text nodes
    • There are no child text nodes... no output.
    • The file is now exhausted.


    A more selective scan is more fruitful:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. QFile in("categories-file.xml");
    9. if (in.open(QIODevice::ReadOnly)) {
    10. QXmlStreamReader s(&in);
    11. while (!s.atEnd()) {
    12. s.readNext();
    13. if (s.isStartElement() && s.name() == "en_main") {
    14. qDebug() << s.name();
    15. qDebug() << s.readElementText();
    16. }
    17. }
    18. }
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 
    output:
    Qt Code:
    1. "en_main"
    2. "Automotive"
    3. "en_main"
    4. "Community (The Big Heart)"
    5. "en_main"
    6. "Electronics & HiTech"
    7. "en_main"
    8. "Events & Announcements"
    9. "en_main"
    10. "Hi Tech"
    11. "en_main"
    12. "Hobbies & Collectibles"
    13. ...
    To copy to clipboard, switch view to plain text mode 

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

    Ceaser88 (26th July 2011)

Similar Threads

  1. Reading and rereading a file with QXmlStreamReader
    By TheRonin in forum Qt Programming
    Replies: 14
    Last Post: 30th April 2015, 14:04
  2. QTextEdit <body> tags text only
    By certqt in forum Qt Programming
    Replies: 3
    Last Post: 22nd January 2011, 13:11
  3. how to use html tags(rich text) in QTableWidgetHeaderItem
    By ansmehta in forum Qt Programming
    Replies: 0
    Last Post: 13th December 2010, 10:20
  4. Replies: 0
    Last Post: 26th May 2010, 19:01
  5. Getting All Text Between Tags in XML ?
    By seanmu13 in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2007, 16:29

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.