PDA

View Full Version : Read local xml file



KeineAhnung
7th December 2015, 16:34
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 (http://doc.qt.io/qt-5/qtqml-xmlhttprequest-get-qml.html)) 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?


<xml>
<termin type="1">
<datum>1449939600</datum>
<art>BA</art>
<thema>Feier</thema>
<anmerkung/>
</termin>
<termin type="2">
<datum>1450458000</datum>
<art>Supervision</art>
<thema>Supervision</thema>
<anmerkung/>
</termin>
</xml>


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?

anda_skoa
7th December 2015, 17:49
The easiest way from QML is XmlListModel.

Cheers,
_

KeineAhnung
7th December 2015, 17:53
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:


console.log(a.childNodes[ii].childNodes[0].firstChild.nodeValue);

anda_skoa
7th December 2015, 19:17
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,
_

KeineAhnung
7th December 2015, 20:45
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.

anda_skoa
8th December 2015, 10:45
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.



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



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,
_

KeineAhnung
8th December 2015, 18:22
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.formatDateTime(new Date(timeStamp,"dd.MM.yyyy - hh:mm" );

Is this a known issue?

anda_skoa
8th December 2015, 19:20
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.


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.



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.formatDateTime(new Date(timeStamp,"dd.MM.yyyy - hh:mm" );

Is this a known issue?
The timeStamp value being the same in both cases?

Cheers,
_

KeineAhnung
8th December 2015, 19:52
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:


while(!xml.atEnd() && !xml.hasError())
{
/* Read next element.*/
QXmlStreamReader::TokenType token = xml.readNext();
/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument)
continue;

/* If token is StartElement, we'll see if we can read it.*/
if(token == QXmlStreamReader::StartElement) {
if(xml.name() == "email") {
ui->listWidget->addItem("Element: "+xml.name().toString());
continue;
}
}
}
/* Error handling. */
if(xml.hasError())
QMessageBox::critical(this, "QXSRExample::parseXML", xml.errorString(), QMessageBox::Ok);

//resets its internal state to the initial state.
xml.clear();
}

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.

ChrisW67
8th December 2015, 19:57
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"?

KeineAhnung
8th December 2015, 20:01
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.

anda_skoa
8th December 2015, 22:26
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


ui->listWidget->addItem("Element: "+xml.name().toString());




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,
_