XmlListModel read an xml file qml
-
Hello,
I am a beginner in Qml / QtQuick.
I have an XML file (which I put the beginning hereafter) but I can not read it in my program qml ...
I do not know what to put in source: neither in query: and in each XmlRole {}, I do not know what to put in query: ...Thank you for your help
Top of the xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- - phpMyAdmin XML Dump - version 4.0.10deb1 - http://www.phpmyadmin.net - - Host: localhost - Generation Time: May 12, 2014 at 10:14 PM - Server version: 5.5.37-0ubuntu0.14.04.1 - PHP Version: 5.5.9-1ubuntu4 -->
<pma_xml_export xmlns:pma="http://www.phpmyadmin.net/some_doc_url/" version="1.0">
<!-- - Structure schemas -->
pma:structure_schemas
<pma:database charset="latin1" collation="latin1_swedish_ci" name="kemi_fr">
<pma:table name="atome"> CREATE TABLEatome
(id
int(10) unsigned NOT NULL AUTO_INCREMENT,nom
varchar(13) DEFAULT NULL,slug
varchar(13) DEFAULT NULL,electron
varchar(255) NOT NULL,numero
int(10) unsigned DEFAULT NULL,symbole
varchar(6) NOT NULL,info_groupe
varchar(10) NOT NULL,info_periode
varchar(10) NOT NULL,info_bloc
varchar(10) NOT NULL,masse_volumique
varchar(255) NOT NULL,cas
varchar(255) NOT NULL,einecs
varchar(255) NOT NULL,masse_atomique
varchar(255) NOT NULL,rayon_atomique
varchar(255) NOT NULL,rayon_de_covalence
varchar(255) NOT NULL,rayon_de_van_der_waals
varchar(255) NOT NULL,configuration_electronique
varchar(255) NOT NULL,etat_oxydation
varchar(255) NOT NULL,decouverte_annee
varchar(255) NOT NULL,decouverte_noms
varchar(255) NOT NULL,decouverte_pays
varchar(255) NOT NULL,electronegativite
varchar(255) NOT NULL,point_de_fusion
varchar(255) NOT NULL,point_d_ebullition
varchar(255) NOT NULL,is_radioactif
tinyint(10) unsigned NOT NULL, PRIMARY KEY (id
) ) ENGINE=InnoDB AUTO_INCREMENT=119 DEFAULT CHARSET=utf8; </pma:table>
</pma:database>
</pma:structure_schemas>
<!-- - Database: 'kemi_fr' -->
<database name="kemi_fr">
<!-- Table atome -->
<table name="atome">
<column name="id">1</column>
<column name="nom">Hydrogène</column>
<column name="slug">hydrogene</column>
<column name="electron">1</column>
<column name="numero">1</column>
<column name="symbole">H</column>
<column name="info_groupe">1</column>
<column name="info_periode">1</column>
<column name="info_bloc">s</column>
<column name="masse_volumique">0,08988 g·l-1, 0,0708 g·ml-1 (liquide, -253 °C), 0,0706 g·ml-1 (solide, -262 °C)</column>
<column name="cas">12385-13-6</column>
<column name="einecs"/>
<column name="masse_atomique">1,00794</column>
<column name="rayon_atomique">25 pm</column>
<column name="rayon_de_covalence">0,31 ± 0,05 Å</column>
<column name="rayon_de_van_der_waals">120 pm</column>
<column name="configuration_electronique">1s1</column>
<column name="etat_oxydation">-1, +1</column>
<column name="decouverte_annee">1766</column>
<column name="decouverte_noms">Henry Cavendish</column>
<column name="decouverte_pays">Grande-Bretagne</column>
<column name="electronegativite">2,2</column>
<column name="point_de_fusion"/>
<column name="point_d_ebullition"/>
<column name="is_radioactif">0</column>
</table> -
Hi! I get a parse error for your XML file, but with the following data things work like this:
<?xml version="1.0" encoding="UTF-8"?> <table name="atome"> <column name="id">1</column> <column name="nom">Hydrogène</column> <column name="slug">hydrogene</column> <column name="electron">1</column> <column name="numero">1</column> <column name="symbole">H</column> <column name="info_groupe">1</column> <column name="info_periode">1</column> <column name="info_bloc">s</column> </table>
import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.0 import QtQuick.XmlListModel 2.0 ApplicationWindow { visible: true width: 640 height: 480 XmlListModel { id: model source: "qrc:/data.xml" query: "/table/column" XmlRole { name: "columnName"; query: "string(@name)" } XmlRole { name: "columnValue"; query: "string()" } } ListView { anchors.fill: parent model: model delegate: Text { text: columnName + " = " + columnValue } } }
-
Check the
status
,xml
, anderrorString
properties (see XmlListModel ) to make sure the model has been loaded. -
Here is a complete example: https://drive.google.com/open?id=0B2D1UtsPfTx-b080YjdtRkE4b3c
You didn't say what Qt version you're using; the example works with 5.8, see if you can make the necessary changes. -
Ok thanks @Wieland but now if my xml file is like this:
<?xml version="1.0" encoding="utf-8"?> <table name="atome"> <column name="id">1</column> <column name="nom">Hydrogène</column> <column name="slug">hydrogene</column> <column name="electron">1</column> <column name="numero">1</column> <column name="symbole">H</column> <column name="info_groupe">1</column> <column name="info_periode">1</column> <column name="info_bloc">s</column> <column name="masse_volumique">0,08988 g·l-1,0,0708 g·ml-1 (liquide, -253°C),0,0706g·ml-1 (solide, -262 °C)</column> <column name="cas">12385-13-6</column> <column name="einecs"></column> <column name="masse_atomique">1,00794</column> <column name="rayon_atomique">25 pm</column> <column name="rayon_de_covalence">0,31 ± 0,05 Å</column> <column name="rayon_de_van_der_waals">120 pm</column> <column name="configuration_electronique">1s1</column> <column name="etat_oxydation">-1, +1</column> <column name="decouverte_annee">1766</column> <column name="decouverte_noms">Henry Cavendish</column> <column name="decouverte_pays">Grande-Bretagne</column> <column name="electronegativite">2,2</column> <column name="point_de_fusion"></column> <column name="point_d_ebullition"></column> <column name="is_radioactif">0</column> </table> <table name="atome"> <column name="id">2</column> <column name="nom">Hélium</column> <column name="slug">helium</column> <column name="electron">2</column> <column name="numero">2</column> <column name="symbole">He</column> <column name="info_groupe">18</column> <column name="info_periode">1</column> <column name="info_bloc">s</column> <column name="masse_volumique">0,1785 g·l-1 (0 °C, 1 atm);0,125 g·ml-1 (liquide, -268,93 °C)</column> <column name="cas">12385-13-6</column> <column name="einecs">231-168-5</column> <column name="masse_atomique">4,002602</column> <column name="rayon_atomique">128 pm (31 pm)</column> <column name="rayon_de_covalence">28 pm</column> <column name="rayon_de_van_der_waals">140 pm</column> <column name="configuration_electronique">1s2</column> <column name="etat_oxydation">0</column> <column name="decouverte_annee">1895</column> <column name="decouverte_noms">Jules Janssen, Joseph Norman Lockyer</column> <column name="decouverte_pays">Grande-Bretagne</column> <column name="electronegativite"></column> <column name="point_de_fusion"></column> <column name="point_d_ebullition">-268,93 °C</column> <column name="is_radioactif">0</column> </table> <table name="atome"> <column name="id">3</column> <column name="nom">Lithium</column> <column name="slug">lithium</column> <column name="electron">2|1</column> <column name="numero">3</column> <column name="symbole">Li</column> <column name="info_groupe">1</column> <column name="info_periode">2</column> <column name="info_bloc">s</column> <column name="masse_volumique">0,534 g·cm-3 (20 °C)</column> <column name="cas">7439-93-2</column> <column name="einecs">231-102-5</column> <column name="masse_atomique">6,941</column> <column name="rayon_atomique">145 pm (167 pm)</column> <column name="rayon_de_covalence">1,28 ± 0,07 Å</column> <column name="rayon_de_van_der_waals">182 pm</column> <column name="configuration_electronique">[He] 2s1</column> <column name="etat_oxydation">+1</column> <column name="decouverte_annee">1817</column> <column name="decouverte_noms">Johan August Arfwedson</column> <column name="decouverte_pays">Suède</column> <column name="electronegativite">0,98</column> <column name="point_de_fusion">180,5 °C</column> <column name="point_d_ebullition">1 342 °C</column> <column name="is_radioactif">0</column> </table>
So I have several table "atome" ... How should I do to display just some columns? For example I just want to display " electron of lithium (id: 3)" ...
thanks you
Ps: I have the versions 5.6 -> 5.9 -
First thing is, your XML file is invalid, you need a top-level element, lets call it "root":
<?xml version="1.0" encoding="utf-8"?> <root> <table>...</table> <table>...</table> <table>...</table> </root>
For example I just want to display " electron of lithium (id: 3)"
XmlListModel { ... query: "/root/table/column[@name='id' and text()=3]/parent::table/column[@name='electron']" ... }
w3schools has two tutorials that might be worth reading: XML Tutorial, XPath Tutorial.