[solved] Getting single elements from XML-File
-
I want to extract several single elements from this XML-structure:
@
<?xml version="1.0" ?>
<program><meta>
<name>somename</name>
<description>descriptiontext</description>
<date></date>
</meta><parameters>
<parameter id = "z">
<description>some useful parameter</description>
<type id="integer">
<default>5</default>
<min>0</min>
<max>10</max>
</type>
</parameter><parameter id="fast">
<description>should it be fast?</description>
<type id = "bool">
<default>1</default>
</type></parameter>
</parameters><code>
important text
</code>
</program>
@This means, that i want to read "program/meta/name", "programm/meta/description", "program/code".
Each into a separate textbox (QML). Is this possible without creating a XMLlistmodel for each item i want to extract? -
The easyest way is using XmlListModel and changing their query for each "section" imho
-
I expected that.
Thanks for your reply -
In my application the user can select a file from a folderlistmodel an in the onSourceChanged signal handler I give my XmlListModel the path to the xml-file.
How can I change the query to access the other "sections" of the xml-file like described above?
I think that this is not possible with signals because everything is linked together
Is it better to solve the whole XML thing in C++ and not in QML?@ Browser{
onSourceChanged: {
model.source = source
}
}XmlListModel { id: model source: "" query: "/program/meta" XmlRole { name: "name"; query: "name/string()" } XmlRole { name: "descr"; query: "description/string()" } } ListView { delegate: Text { text: name + " : " + descr } }
@
I want to add an other ListView or Text component to get the field in "program/code" (from post 1) as well. -
Try this:
@
XmlListModel {property string currentQuery id: model source: "your_file_or_url.xml" query: "" XmlRole { name: "name"; query: "name/string()" } XmlRole { name: "descr"; query: "description/string()" } // and add all the variables who can be extracted with XmlRole, when they doesnt found just return a undefined data onCurrentQueryChanged: { model.query = mode.currentQuery model.reload(); } } ListView { delegate: Text { text: name + " : " + descr } }
@
Now use your selector to send the
@
model.currentQuery = "/program/meta"
or
model.currentQuery = "/program/parameters/parameter"
or
model.currentQuery = "/program/code"
@So with only one model you can extract all the information.
-
Thanks for your answer,
but I want to display these querys at one time.
Something like this:
@
model.currentQuery = "/program/meta"
then / and
model.currentQuery = "/program/parameters/parameter"
then/ and
model.currentQuery = "/program/code"
@..to fill out a form and to display the querys (have only one entry) in text elements
@
Text { text: "Name: " + name }
Text { text: "Description: " + descr }
Text { text: "Code: " + code }
...
@Maybe this is only possible with the help of some string properties? If I change the query, some text elements would certainly only show undefined data. Maybe Qts DOM / SAX handler can get these data on an easier way.
-
U made something like this, but using the OfflineStorage database to store the results after collect it.
-
Thanks, I will try this.
I hope this is not an overkill. Since I just want to get 4-5 elements from this xml file.
But maybe this ist not as simple as I thought. -
This is a example i made long time ago for a personal project:
@XmlListModel {
id: xmlProductsModelproperty bool debug : true source: "http://fakeaddress.com/rss.xml" query: "/rss/products/product" XmlRole { name: "id"; query: "id/string()" } XmlRole { name: "brand"; query: "brand/string()" } XmlRole { name: "comment"; query: "comment/string()" } XmlRole { name: "description"; query: "description/string()" } XmlRole { name: "price"; query: "precio/string()" } XmlRole { name: "imageUrl"; query: "imageFile/string()" } XmlRole { name: "categoryId"; query: "catId/string()" } /** * When the model is loaded, insert it into the database and the sqlite model */ onStatusChanged : { if (xmlProductsModel.status == XmlListModel.Ready) { var db = openDatabaseSync("Database", "1.0", "TestDatabase", 3000000); db.transaction( function(tx) { tx.executeSql('DELETE FROM "products"'); for (var i = 0; i < count; i++) { if(debug) console.debug("Adding new product: (" + get(i).id + ") " + get(i).description ); tx.executeSql('INSERT INTO products(id, id_cat_prod, brand, description, price, comment, image)' + 'values(?, ?, ?, ?, ?, ?, ?)', [get(i).id, get(i).categoryId, get(i).brand, get(i).description, get(i).price, get(i).comment, get(i).imageUrl ]); } }) model.updatedSuccessful() xmlProductsModel.destroy(); } }
}
@ -
This seems to work best for me:
"How to Read and Write in XML Files with Qt/C++":https://sites.google.com/a/embeddedlab.org/community/technical-articles/qt/qt-posts/howtoreadandwriteinxmlfileswithqtcI only have to write a connection to my GUI written in QML. But this shouldn't be a problem.