Results 1 to 5 of 5

Thread: Error reading local XML file with XmlListModel

  1. #1
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Error reading local XML file with XmlListModel

    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:
    Qt Code:
    1. import QtQuick 2.5
    2. import QtQuick.Window 2.2
    3. import QtQuick.Controls 1.4
    4. import QtQuick.Layouts 1.1
    5. import QtQuick.XmlListModel 2.0
    6.  
    7. import com.brkaubing 1.0
    8.  
    9. ApplicationWindow {
    10. visible: true
    11.  
    12. toolBar: ToolBar {
    13. RowLayout {
    14. ToolButton {
    15. text: "Back"
    16. visible: stack.depth > 1
    17.  
    18. onClicked: stack.pop()
    19. }
    20. }
    21. }
    22.  
    23. XmlFileHandler {
    24. id: handler;
    25.  
    26. property bool filesChecked: false
    27. property bool showInternalDates: false
    28. property bool showHvoDates: true
    29. property bool showEventDates: false
    30. property int offset: 0
    31. property string actionItem: ""
    32. property string baItem: ""
    33. property string hvoItem: ""
    34.  
    35. onNextEventsArrayChanged: {
    36. if(fileStat > 0){
    37. for (var i = 0; i < nextEventsArray.length; i++){
    38. console.log(i+": "+nextEventsArray[i])
    39. }
    40. if(showInternalDates)
    41. offset = nextEventsArray[24]
    42.  
    43. actionItem = "am: "+handler.nextEventsArray[20]+" Uhr\n"+handler.nextEventsArray[22]
    44. baItem = "am: "+handler.nextEventsArray[handler.offset]+" Uhr
    45. Thema: "+handler.nextEventsArray[handler.offset+2]
    46. hvoItem = "am: "+handler.nextEventsArray[8]+" Uhr"
    47.  
    48. if(fileStat > 0 && handler.nextEventsArray[20].isEmpty){
    49. var today = new Date()
    50. var tmp = handler.nextEventsArray[20].split('.')
    51. var newEventDate = new Date(("20"+tmp[2]).substring(0,4)*1,tmp[1]*1-1,tmp[0]*1,12,0,0,0)
    52. showEventDates = newEventDate>today+21*24*3600000 ? false:true
    53. }else{
    54. showEventDates = false
    55. }
    56. }
    57. }
    58. }
    59.  
    60. Component.onCompleted: {
    61. if(!handler.filesChecked){
    62. handler.load(path, file);
    63. handler.filesChecked = true;
    64. }
    65. }
    66.  
    67. StackView {
    68. id: stack
    69. initialItem: firstPage
    70. anchors.fill: parent
    71.  
    72. FirstPage {
    73. id:firstPage
    74. }
    75.  
    76. Component {
    77. id:planPage
    78.  
    79. Flickable {
    80. id: planView
    81. anchors.fill: parent
    82.  
    83. ListView {
    84. id: listView
    85. anchors.fill: parent
    86.  
    87. property bool modelDataError: false
    88. property string statusMessage: ""
    89. property string filter: ""
    90. property var date : new Date()
    91. property int now: date.getTime()/1000 - 24*3600 // Termine die schon waren sollen nicht mehr angezeigt werden.
    92.  
    93. model: planModel
    94. delegate: Text {
    95. text: thema
    96. }
    97. }
    98.  
    99. XmlListModel {
    100. id: planModel
    101. source: handler.filePath()
    102. query: "/xml/termin"+listView.filter
    103. XmlRole { name: "type"; query: "@type/number()" }
    104. XmlRole { name: "datum"; query: "datum/string()" }
    105. XmlRole { name: "art"; query: "art/string()" }
    106. XmlRole { name: "thema"; query: "thema/string()" }
    107. XmlRole { name: "anmerkung"; query: "anmerkung/string()"}
    108.  
    109. onStatusChanged: {
    110. listView.modelDataError = false
    111. console.log(planModel.source)
    112. if(status == XmlListModel.Error) {
    113. listView.state = "Offline"
    114. listView.statusMessage = "Ein Fehler ist aufgetreten: " + errorString()
    115. listView.modelDataError = true
    116. console.log("Terminplan: " + listView.statusMessage)
    117. } else if (status == XmlListModel.Ready) {
    118. if(get(0) === undefined){
    119. listView.state = "Offline"
    120. listView.statusMessage = "Die lokalen Daten sind defekt. Bitte starten Sie die App neu."
    121. handler.clear()
    122. listView.modelDataError = true
    123. } else {
    124. listView.state = "Online"
    125. listView.statusMessage = "Aktuelle Daten sind verfügbar. "+now
    126. }
    127. console.log("Terminpaln: "+listView.statusMessage)
    128. } else if (status == XmlListModel.Loading){
    129. listView.state = "Läd..."
    130. listView.statusMessage = "Daten werden geladen."
    131. } else if(status == XmlListModel.Null) {
    132. listView.state = "Loading"
    133. listView.statusMessage = "Forecast data is empty..."
    134. console.log("Terminplan: " + listView.statusMessage)
    135. } else {
    136. listView.modelDataError = fase
    137. console.log("Terminplan: Unklarer Zustand der XML Datei: " + status)
    138. }
    139. }
    140. }
    141. }
    142. }
    143. }
    144. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Error reading local XML file with XmlListModel

    Have you looked for character encoding differences? That can sometimes be a PITA when working cross-platform.

  3. #3
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Error reading local XML file with XmlListModel

    That is a good hint but I do not believe so. The path it not hardcoded, I get it via my c++ class:
    Qt Code:
    1. fileLocation = QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Error reading local XML file with XmlListModel

    Have you tried returning a file URI? I.e. QUrl::fromLocalFile().

    Cheers,
    _

  5. #5
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Error reading local XML file with XmlListModel

    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()
    Qt Code:
    1. return QUrl::fromLocalFile(fileUrl);
    To copy to clipboard, switch view to plain text mode 
    it results in file:///Users/*/Library/Application Support/[AppName]/*.xml
    and that path works OS X and Android.

Similar Threads

  1. XmlListModel
    By mymissing in forum Qt Quick
    Replies: 1
    Last Post: 2nd September 2013, 22:40
  2. Error in reading wav-file header
    By davidlamhauge in forum Qt Programming
    Replies: 5
    Last Post: 18th November 2012, 14:35
  3. Replies: 1
    Last Post: 22nd December 2010, 17:56
  4. unknown error reading svg file
    By RickF in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2010, 02:12
  5. Fatal IO error with QGLWidget and reading a file
    By mcn in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2010, 15:21

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.