×

Discussion Board

Results 1 to 9 of 9

Thread: Reading XML

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    12

    Reading XML

    Two nights I've been trying to read one simple XML file. What I'm doing wrong here? Probably lot?


    Code:
    void MainWindow::read()
    {
        const QString xmlFile("events.xml");
        QString text;
        QFile file(xmlFile);
        if (file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QXmlStreamReader xmlReader(&file);       
            while (!xmlReader.atEnd() && !xmlReader.hasError()) {            
                if (xmlReader.name() == "happening") {
                    text = xmlReader.readElementText();
                    qDebug() << text;
                }        
            }
    
        }
    
    }
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xbel>
    <xbel version="1.0">
        <n1:Happenings file xmlns:n1="Happenings">Something</n1:Happenings file>
        <happening>Some text</happening>
        </xbel>

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Reading XML

    Whatever you do, you probable need to call xmlReader.readNext(); inside while loop.

    Code:
    #include <QtGui>
    #include <QtXml>
    
    int main(int argc, char **argv)
    {
        QCoreApplication app(argc, argv);
    
        QString text;
        QFile file("events.xml");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            qWarning("IO error %i", file.error());
            return 1;
        }
    
        QXmlStreamReader xml(&file);
        while (!xml.atEnd()) {
            switch (xml.readNext()) {
                case QXmlStreamReader::StartElement:
                    qDebug() << "Start element: " << xml.name();
                    break;
                case QXmlStreamReader::Characters:
                    qDebug() << "Characters:" << xml.text();
                    break;
                case QXmlStreamReader::EndElement:
                    qDebug() << "End element: " << xml.name();
                    break;
                default:
                    qDebug() << "Something else: " << xml.tokenString();
            }
        }
        if (xml.hasError()) {
            qDebug() << "Error: " << xml.errorString();
        }
        file.close();
    
        return 0;
    }
    Last edited by divanov; 2011-02-03 at 21:35. Reason: code snipper

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    12

    Re: Reading XML

    Now I think I'm closer. But not yet there...

    Code:
    void MainWindow::read()
    {
        const QString xmlFile("events.xml");
        QString text;
        QFile file(xmlFile);
        if (file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QXmlStreamReader xmlReader(&file);       
            while (!xmlReader.atEnd() && !xmlReader.hasError()) {     
                xmlReader.readNext();
                
                if (xmlReader.name() == "happening") {
                    xmlReader.readElementText();
                    qDebug() << xmlReader.name();
                }        
            }
    
        }
    
    }
    Output shows that it reads still totally wrong elements.

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Reading XML

    Did you try my sample?

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    12

    Re: Reading XML

    I tried. "Xbel" was the only output.

    Edit:

    With this code I get what I want, but also few empty outputs. Next thing is to figure out how I get rid of the empty outputs.

    Code:
    void MainWindow::read()
    {
        const QString xmlFile("events.xml");
        QString text;
        QFile file(xmlFile);
        if (file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QXmlStreamReader xmlReader(&file);       
            while (!xmlReader.atEnd() && !xmlReader.hasError()) {     
                xmlReader.readNext();
                
                if (xmlReader.text() == "happening") {
                    xmlReader.readElementText();
                    qDebug() << xmlReader.text();
                }        
            }
    
        }
    
    }
    Last edited by Anne80; 2011-02-09 at 17:45.

  6. #6
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Reading XML

    I have a bit different output:
    Code:
    $ ./xml
    Something else:  "StartDocument" 
    Something else:  "DTD" 
    Start element:  "xbel" 
    Characters: "
        " 
    Something else:  "Invalid" 
    Error:  "Expected '=', but got '[a-zA-Z]'."

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    12

    Re: Reading XML

    Why this doesn't block out the empty parts?

    Code:
                if (xmlReader.text() == "happening" && xmlReader.text() != "") 
                    xmlReader.readElementText();
                    qDebug() << xmlReader.text();
    Edit: Because my IF is broken. And noticed also that "xmlReader.text() == "happening" " doesn't select the element.
    Last edited by Anne80; 2011-02-09 at 18:39.

  8. #8
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Reading XML

    That code's going to read until it finds an element with text="happening" (the "xmlReader.text() != """ part has no effect), skip the rest of that (the <happening...) element, then report the next text value. The way I read it that should give you "xbel".

    [Actually, I see that there's no reader setting to ignore whitespace. This probably means that every newline is going to be mapped to a whitespace element. So likely the code snippet would just return a newline.]
    Last edited by danhicksbyron; 2011-02-09 at 18:48.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    12

    Re: Reading XML

    The final working solution was this:
    Code:
    while (!xmlReader.atEnd() && !xmlReader.hasError()) {
    
    xmlReader.readNextStartElement();
    
    if (xmlReader.name() == "happening"){
                    happening = xmlReader.readElementText();
                    qDebug() << happening;
    ....
    Thank you for your help and sorry for my unclear questions

Similar Threads

  1. Reading XML files
    By NightStalkerDNS in forum Mobile Java General
    Replies: 1
    Last Post: 2009-12-24, 19:42
  2. Reading from XML into a CArrayFlatFix
    By nisheethpandey in forum Symbian
    Replies: 7
    Last Post: 2009-11-25, 11:58
  3. XML reading
    By manojpaul in forum Mobile Java General
    Replies: 2
    Last Post: 2009-07-10, 12:09
  4. xml reading/writing
    By mdrahman.nokia770 in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2008-07-21, 04:09
  5. Reading of XML.HELP!!
    By nick_santa83 in forum Symbian
    Replies: 1
    Last Post: 2008-02-07, 22:45

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×