Results 1 to 12 of 12

Thread: Read local xml file

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

    Default Read local xml file

    Hi,

    finally I managed to download and save a xml file to my disk. Now I would like to find a specific element in this xml file. I managed to open it in QML using JS (how to) but I am just not able to access the content of the nodes. What is the right syntax to access these information from the for loop?
    Qt Code:
    1. <xml>
    2. <termin type="1">
    3. <datum>1449939600</datum>
    4. <art>BA</art>
    5. <thema>Feier</thema>
    6. <anmerkung/>
    7. </termin>
    8. <termin type="2">
    9. <datum>1450458000</datum>
    10. <art>Supervision</art>
    11. <thema>Supervision</thema>
    12. <anmerkung/>
    13. </termin>
    14. </xml>
    To copy to clipboard, switch view to plain text mode 

    For example console.log(a[ii].getElementsByTagName("datum")[0].childNodes[0].nodeValue); says TypeError: Cannot call method 'getElementsByTagName' of undefined
    What am I doing wrong?
    Last edited by KeineAhnung; 7th December 2015 at 16:41.

  2. #2
    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: Read local xml file

    The easiest way from QML is XmlListModel.

    Cheers,
    _

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

    Default Re: Read local xml file

    Also if I am looking for a specific value in the XML file?

    By the way I just got it. The right syntax to access the datum would be:
    Qt Code:
    1. console.log(a.childNodes[ii].childNodes[0].firstChild.nodeValue);
    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: Read local xml file

    Quote Originally Posted by KeineAhnung View Post
    Also if I am looking for a specific value in the XML file?
    Then the most efficient way is to use the QXmlStreamReader API.
    Creating a full DOM structure just for getting a single value would be quite an overkill

    But of course that requires to have a C++ application core available.
    Since you pointed to the XML RPC Request example I guess that is not the case for you, otherwise you would not be doing data processing in JavaScript

    Cheers,
    _

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

    Default Re: Read local xml file

    I am working on a Sailfish OS app so I could use C++. For the moment it is working and my XML file will have not more than 100 entries. But I will look at QXmlStreamReader.
    Are you familiar with Sailfish OS?

    Thanks for the hint.

  6. #6
    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: Read local xml file

    Quote Originally Posted by KeineAhnung View Post
    I am working on a Sailfish OS app so I could use C++.
    Then the usual recommendation stands: don't use JavaScript for data processing that is not UI related.
    Keep JS usage as minimal as possible.

    Quote Originally Posted by KeineAhnung View Post
    For the moment it is working and my XML file will have not more than 100 entries.
    So you need one value from each of these 100 entries?
    And you are not needing these 100 values as a model?

    - Extractnig values into a list or vector: QXmlStreamReader
    - Needing the values only as a model in QML: XmlListModel

    Quote Originally Posted by KeineAhnung View Post
    Are you familiar with Sailfish OS?
    Haven't worked with it myself but I have heard about it.
    It is the system used by Jolla, an embedded Linux using a Qt base application framework, with Wayland as the display technology.

    Cheers,
    _

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

    Default Re: Read local xml file

    The XML file contains dates and I want to display upcoming date on the first page. The JS is very fast I cannot see any delay but I will rework my xmlFileHandler class and add the option to extract the next event. Understand I correctly that you would put the XML file content into a QVector and than search through that?

    By the way, I noticed a difference between JS and XmlListView. Within XmlListView my timestamps are not displayed correctly. I use the exact same code to convert the same timeStamp from the XML file. 6pm is displayed correctly but all other times are off a few seconds.
    Qt Code:
    1. Qt.formatDateTime(new Date(timeStamp,"dd.MM.yyyy - hh:mm" );
    To copy to clipboard, switch view to plain text mode 
    Is this a known issue?

  8. #8
    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: Read local xml file

    Quote Originally Posted by KeineAhnung View Post
    The JS is very fast I cannot see any delay
    Avoiding JS it not only about performance, it is mostly about correctness and code maintainability.

    Quote Originally Posted by KeineAhnung View Post
    Understand I correctly that you would put the XML file content into a QVector and than search through that?
    Yes, if you extract a list of timestamps, I would store them in a vector or list of QDateTime.

    Quote Originally Posted by KeineAhnung View Post
    By the way, I noticed a difference between JS and XmlListView. Within XmlListView my timestamps are not displayed correctly. I use the exact same code to convert the same timeStamp from the XML file. 6pm is displayed correctly but all other times are off a few seconds.
    Qt Code:
    1. Qt.formatDateTime(new Date(timeStamp,"dd.MM.yyyy - hh:mm" );
    To copy to clipboard, switch view to plain text mode 
    Is this a known issue?
    The timeStamp value being the same in both cases?

    Cheers,
    _

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

    Default Re: Read local xml file

    Yes it is the same timestamp. I have the date in two locations. On the first page it is correct and on the full list page it is off.

    How would you put the xml file into the vector? All code examples I have found look like this:
    Qt Code:
    1. while(!xml.atEnd() && !xml.hasError())
    2. {
    3. /* Read next element.*/
    4. QXmlStreamReader::TokenType token = xml.readNext();
    5. /* If token is just StartDocument, we'll go to next.*/
    6. if(token == QXmlStreamReader::StartDocument)
    7. continue;
    8.  
    9. /* If token is StartElement, we'll see if we can read it.*/
    10. if(token == QXmlStreamReader::StartElement) {
    11. if(xml.name() == "email") {
    12. ui->listWidget->addItem("Element: "+xml.name().toString());
    13. continue;
    14. }
    15. }
    16. }
    17. /* Error handling. */
    18. if(xml.hasError())
    19. QMessageBox::critical(this, "QXSRExample::parseXML", xml.errorString(), QMessageBox::Ok);
    20.  
    21. //resets its internal state to the initial state.
    22. xml.clear();
    23. }
    To copy to clipboard, switch view to plain text mode 
    So if I go through the XML one by one I could also stop when I found what I was looking for without putting all elements into the vector.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Read local xml file

    The formatDateTime() call you gives you a string representing the time stamp to an accuracy of minutes. How do you tell the value is "off a few seconds"?

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

    Default Re: Read local xml file

    For debugging I also displayed the seconds and saw that different timeStamps have different offsets. So 7pm is always off 16s 6pm is okay 19:30 is off 22s and so on.

  12. #12
    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: Read local xml file

    Quote Originally Posted by KeineAhnung View Post
    How would you put the xml file into the vector?
    Not the XML file, the data you are interested in.
    In the example that would be instead of line
    Qt Code:
    1. ui->listWidget->addItem("Element: "+xml.name().toString());
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by KeineAhnung View Post
    So if I go through the XML one by one I could also stop when I found what I was looking for without putting all elements into the vector.
    Of course.
    That's one of the great things about QXmlStreamReader.

    Cheers,
    _

Similar Threads

  1. QWebEngineSettings allow local file write?
    By RolandHughes in forum Qt Programming
    Replies: 3
    Last Post: 25th June 2015, 15:56
  2. read out local printer
    By Qiieha in forum Qt Programming
    Replies: 1
    Last Post: 2nd November 2011, 19:15
  3. local file path
    By sky in forum Newbie
    Replies: 3
    Last Post: 20th January 2011, 08:59
  4. Replies: 1
    Last Post: 22nd December 2010, 17:56
  5. Password on local file/folder
    By icebox25 in forum Qt Programming
    Replies: 3
    Last Post: 13th April 2007, 16:33

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.