[Solved]QXmlStreamReader Problem



  • Hi,

    I'am having trouble reading from an Xml file.

    @QFile file("forecast.xml");
    file.open(QIODevice::ReadOnly);

    QXmlStreamReader xml(&file);
    while (!xml.atEnd())
    {
    xml.readNext();
    name = xml.name().toString();
    }
    if (xml.hasError())
    {
    name = xml.errorString();
    }
    file.close();@

    Error String is premature end of document.
    I got the Feeling that the file isnt read.

    Thanks,

    Regads,

    Maddin


  • Lifetime Qt Champion

    Hi,

    Check what the open call returns, it might have failed.



  • Hi,

    New Code:

    @bool testOpen;

    QString name;
    QFile file("forecast.xml");

    file.open(QIODevice::ReadOnly);

    if(file.open(QIODevice::ReadOnly)) {
    testOpen = true;
    } else {
    testOpen = false;
    }@

    The result is false. So You are right. I got the xml file in my src directory. Is it the wrong Directory ?

    Thx



  • This is the xml File.

    @<?xml version="1.0" encoding="UTF-8"?>
    <weather>
    <curren_weather>
    <humidity>80</humidity>
    <temp_unit>c</temp_unit>
    <wind>
    <speed>5</speed>
    <dir>NNE</dir>
    <wind_unit>kph</wind_unit>
    </wind>
    <temp>2</temp>

    <pressure>1025</pressure>
    <weather_text>Cloudy</weather_text>
    <weather_code>3</weather_code>
    </curren_weather>
    <forecast>
    <date>2013-02-25</date>
    <temp_unit>c</temp_unit>
    <day_max_temp>5</day_max_temp>
    <night_min_temp>2</night_min_temp>
    <day>
    <weather_text>Overcast skies</weather_text>
    <weather_code>3</weather_code>
    <wind>
    <speed>22</speed>
    <dir>NNE</dir>
    <dir_degree>21</dir_degree>
    <wind_unit>kph</wind_unit>
    </wind>
    </day>
    <night>
    <weather_text>Overcast skies</weather_text>
    <weather_code>3</weather_code>
    <wind>
    <speed>22</speed>
    <dir>NNE</dir>
    <dir_degree>11</dir_degree>
    <wind_unit>kph</wind_unit>
    </wind>
    </night>
    </forecast>
    <forecast>
    <date>2013-02-26</date>
    <temp_unit>c</temp_unit>
    <day_max_temp>5</day_max_temp>
    <night_min_temp>1</night_min_temp>
    <day>
    <weather_text>Light drizzle</weather_text>
    <weather_code>51</weather_code>
    <wind>
    <speed>22</speed>
    <dir>ENE</dir>
    <dir_degree>58</dir_degree>
    <wind_unit>kph</wind_unit>
    </wind>
    </day>
    <night>
    <weather_text>Mist</weather_text>
    <weather_code>10</weather_code>
    <wind>
    <speed>14</speed>
    <dir>NNE</dir>
    <dir_degree>29</dir_degree>
    <wind_unit>kph</wind_unit>
    </wind>
    </night>
    </forecast>
    </weather>@

    Maybe it helps.


  • Lifetime Qt Champion

    Depends on what you mean by wrong.

    Your application working directory is in the build directory so it won't find forecast.xml.

    You need to use the complete path to forecast.xml ie @QFile file("/home/myusername/mycodepath/forecast.xml")@

    Or have it in the same folder than application.



  • The forecast.xml file is in the folder of the application and in the src folder.
    Their are other folder like assets, translations, x86 and .settings

    its an Blackberry 10 Native application

    In the IDE iam clicking rightclick on the xml file -> Properties and their is a path but copying it wont work.
    I mean copying works but when I past it, the open call is still false

    I copying the file to every folder in my project.

    Maybe their is somethin wrong with the open function call

    Well I see, You're getting out of ideas. Thanks for the help anyway.


  • Lifetime Qt Champion

    forecast.xml should not be were your code is but were your binary is.
    If you want it somewhere else, give the complete path to QFile.
    If the file is not meant to change, you can also embedded it using qrc.



  • What do you mean with Binary? where the c++ files are?


  • Lifetime Qt Champion

    No, the c++ file are the sources, the binary is the executable file that gets generated from the sources.



  • Ah, Ok. Almost forgot. Well thier is a folder called Binary. Tried to copy the forecast.xml thier but with no succes for the open call. It is still false. I got the feeling that Windows is getting in the way with the right path. Iam set up a linux and try my luck their.


  • Lifetime Qt Champion

    Are you sure that the file is put with the binary ? Do you run the binary on windows or on your target ?



  • I run the binary on windows. I use a simulator.

    This is my project tree and in the folder x86 are the binaries i think.

    !http://img201.imageshack.us/img201/7232/projecttree.jpg(Project Tree)!

    I copied the file to C:/forecast.xml and tried

    QFile file("C:/forecast.xml");

    with no success



  • I dont know anymore. I reply the current sorce code. Its messy hope you dont mind.

    @// Default empty project template

    #include "Test.hpp"

    #include <bb/cascades/Application>
    #include <bb/cascades/QmlDocument>
    #include <bb/cascades/AbstractPane>

    #include <bb/cascades/GroupDataModel>
    #include <bb/cascades/ListView>

    #include <stdio.h>

    #include <bb/cascades/Color>

    #include <bb/data/XmlDataAccess.hpp>

    using namespace bb::cascades;

    Test::Test(bb::cascades::Application *app) :
    QObject(app) {

    bool testOpen = true;

    QString name;
    QFile file(QString("/test/src/forecast.xml"));

    //file.open(QIODevice::ReadOnly);

    /if() {
    testOpen = true;
    } else {
    testOpen = false;
    }
    /

    //file.open(QIODevice::ReadOnly);

    QXmlStreamReader xml(&file);
    //while (!xml.atEnd())
    //{
    xml.readNext();
    name = xml.name().toString();
    //}
    if (xml.hasError())
    {
    name = xml.errorString();
    }
    //file.close();

    //QFile file("forecast.xml");
    /*QFile file("forecast.xml");

    QXmlStreamReader xml(&file);

    //while(!xml.atEnd()) {
    xml.readNext();
    //if(xml.readNextStartElement()) {
    name = xml.name().toString();
    //}
    qDebug() << xml.lineNumber() << xml.name().toString();

    //}

    if (xml.hasError()) {
    name = xml.errorString();

    }*/

    // create scene document from main.qml asset
    // set parent to created document to ensure it exists for the whole application lifetime
    QmlDocument qml = QmlDocument::create("asset:///main.qml").parent(this);
    QDeclarativePropertyMap
    propertyMap = new QDeclarativePropertyMap;

    //insert string mystring and 519-555-0199 into name and phone
    propertyMap->insert("name", QVariant(file.open(QIODevice::ReadOnly | QIODevice::Text)));
    propertyMap->insert("phone", QVariant(QString("519-555-0199")));

    qml->setContextProperty("propertyMap", propertyMap);

    // create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    // set created root object as a scene
    app->setScene(root);
    }

    @


  • Lifetime Qt Champion

    If your software runs on the emulator, put forecast.xml in the same directory as the executable on the emulator



  • Hi,

    It was the wrong path after all.

    I did

    QFile file("app/native/assets/forecast.xml");

    and now its working.

    Thx for the help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.