PDA

View Full Version : Error reading local XML file with XmlListModel



KeineAhnung
2nd March 2016, 20:14
Hi,

I am still trying to port my Sailfish App to Android. So far I managed to download a XML file from the web and to store it locally. Using my C++ file handler I am able to read the file but I get an "unknown error" when I try to read the XML file with XmlListModel.

Here is my Code:


import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import QtQuick.XmlListModel 2.0

import com.brkaubing 1.0

ApplicationWindow {
visible: true

toolBar: ToolBar {
RowLayout {
ToolButton {
text: "Back"
visible: stack.depth > 1

onClicked: stack.pop()
}
}
}

XmlFileHandler {
id: handler;

property bool filesChecked: false
property bool showInternalDates: false
property bool showHvoDates: true
property bool showEventDates: false
property int offset: 0
property string actionItem: ""
property string baItem: ""
property string hvoItem: ""

onNextEventsArrayChanged: {
if(fileStat > 0){
for (var i = 0; i < nextEventsArray.length; i++){
console.log(i+": "+nextEventsArray[i])
}
if(showInternalDates)
offset = nextEventsArray[24]

actionItem = "am: "+handler.nextEventsArray[20]+" Uhr\n"+handler.nextEventsArray[22]
baItem = "am: "+handler.nextEventsArray[handler.offset]+" Uhr
Thema: "+handler.nextEventsArray[handler.offset+2]
hvoItem = "am: "+handler.nextEventsArray[8]+" Uhr"

if(fileStat > 0 && handler.nextEventsArray[20].isEmpty){
var today = new Date()
var tmp = handler.nextEventsArray[20].split('.')
var newEventDate = new Date(("20"+tmp[2]).substring(0,4)*1,tmp[1]*1-1,tmp[0]*1,12,0,0,0)
showEventDates = newEventDate>today+21*24*3600000 ? false:true
}else{
showEventDates = false
}
}
}
}

Component.onCompleted: {
if(!handler.filesChecked){
handler.load(path, file);
handler.filesChecked = true;
}
}

StackView {
id: stack
initialItem: firstPage
anchors.fill: parent

FirstPage {
id:firstPage
}

Component {
id:planPage

Flickable {
id: planView
anchors.fill: parent

ListView {
id: listView
anchors.fill: parent

property bool modelDataError: false
property string statusMessage: ""
property string filter: ""
property var date : new Date()
property int now: date.getTime()/1000 - 24*3600 // Termine die schon waren sollen nicht mehr angezeigt werden.

model: planModel
delegate: Text {
text: thema
}
}

XmlListModel {
id: planModel
source: handler.filePath()
query: "/xml/termin"+listView.filter
XmlRole { name: "type"; query: "@type/number()" }
XmlRole { name: "datum"; query: "datum/string()" }
XmlRole { name: "art"; query: "art/string()" }
XmlRole { name: "thema"; query: "thema/string()" }
XmlRole { name: "anmerkung"; query: "anmerkung/string()"}

onStatusChanged: {
listView.modelDataError = false
console.log(planModel.source)
if(status == XmlListModel.Error) {
listView.state = "Offline"
listView.statusMessage = "Ein Fehler ist aufgetreten: " + errorString()
listView.modelDataError = true
console.log("Terminplan: " + listView.statusMessage)
} else if (status == XmlListModel.Ready) {
if(get(0) === undefined){
listView.state = "Offline"
listView.statusMessage = "Die lokalen Daten sind defekt. Bitte starten Sie die App neu."
handler.clear()
listView.modelDataError = true
} else {
listView.state = "Online"
listView.statusMessage = "Aktuelle Daten sind verfügbar. "+now
}
console.log("Terminpaln: "+listView.statusMessage)
} else if (status == XmlListModel.Loading){
listView.state = "Läd..."
listView.statusMessage = "Daten werden geladen."
} else if(status == XmlListModel.Null) {
listView.state = "Loading"
listView.statusMessage = "Forecast data is empty..."
console.log("Terminplan: " + listView.statusMessage)
} else {
listView.modelDataError = fase
console.log("Terminplan: Unklarer Zustand der XML Datei: " + status)
}
}
}
}
}
}
}


The path is correct and pointing to the right file. I use the same code on Sailfish and there it is working. Where could I look for my error?

sedi
7th March 2016, 21:17
Have you looked for character encoding differences? That can sometimes be a PITA when working cross-platform.

KeineAhnung
8th March 2016, 18:52
That is a good hint but I do not believe so. The path it not hardcoded, I get it via my c++ class:


fileLocation = QDir(QStandardPaths::writableLocation(QStandardPat hs::DataLocation));

anda_skoa
8th March 2016, 22:11
Have you tried returning a file URI? I.e. QUrl::fromLocalFile().

Cheers,
_

KeineAhnung
9th March 2016, 19:55
Thanks for the hint, I got it working.

Initially I returned the fileLocation as QString resulting in qrc:/Users/*/Library/Application Support/[AppName]/*.xml
If I return the path via fromLocalFile()

return QUrl::fromLocalFile(fileUrl);
it results in file:///Users/*/Library/Application Support/[AppName]/*.xml
and that path works OS X and Android.