Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [solved] Getting single elements from XML-File
Forum Updated to NodeBB v4.3 + New Features

[solved] Getting single elements from XML-File

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 2 Posters 6.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rocken
    wrote on 30 Jan 2012, 13:13 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GentooXativa
      wrote on 30 Jan 2012, 13:22 last edited by
      #2

      The easyest way is using XmlListModel and changing their query for each "section" imho

      Jose Vicente Giner Sanchez - Senior Mobile Developer

      www.gigigo.com

      C/ Dr. Zamenhof 36bis, 1ºA 28027 Madrid
      T: +34 917431436

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rocken
        wrote on 30 Jan 2012, 13:31 last edited by
        #3

        I expected that.
        Thanks for your reply

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rocken
          wrote on 30 Jan 2012, 14:45 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            GentooXativa
            wrote on 30 Jan 2012, 14:56 last edited by
            #5

            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.

            Jose Vicente Giner Sanchez - Senior Mobile Developer

            www.gigigo.com

            C/ Dr. Zamenhof 36bis, 1ºA 28027 Madrid
            T: +34 917431436

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rocken
              wrote on 31 Jan 2012, 06:36 last edited by
              #6

              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.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                GentooXativa
                wrote on 31 Jan 2012, 06:39 last edited by
                #7

                U made something like this, but using the OfflineStorage database to store the results after collect it.

                Jose Vicente Giner Sanchez - Senior Mobile Developer

                www.gigigo.com

                C/ Dr. Zamenhof 36bis, 1ºA 28027 Madrid
                T: +34 917431436

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rocken
                  wrote on 31 Jan 2012, 07:08 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    GentooXativa
                    wrote on 31 Jan 2012, 08:05 last edited by
                    #9

                    This is a example i made long time ago for a personal project:
                    @

                    XmlListModel {
                    id: xmlProductsModel

                    property 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();
                        }
                    }
                    

                    }
                    @

                    Jose Vicente Giner Sanchez - Senior Mobile Developer

                    www.gigigo.com

                    C/ Dr. Zamenhof 36bis, 1ºA 28027 Madrid
                    T: +34 917431436

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Rocken
                      wrote on 31 Jan 2012, 12:28 last edited by
                      #10

                      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/howtoreadandwriteinxmlfileswithqtc

                      I only have to write a connection to my GUI written in QML. But this shouldn't be a problem.

                      1 Reply Last reply
                      0

                      1/10

                      30 Jan 2012, 13:13

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved