Store XML data in a String Array using ListView



  • I have and xml string.

    
    <Output>
         <user>
         <name>Allan</name>
         <surname>Smith</surname>
         <age>64<age>
      </user>
      <user>
         <name>Bradley</name>
         <surname>Carrel</surname>
         <age>58<age>
      </user>
      <user>
         <name>Sam</name>
         <surname>Johnson</surname>
         <age>24<age>
      </user>
    </Output>
    

    I want to store the xml data inside three String arrays, eg names[], surnames[], ages[].

    I am using QML's list view to read the xml data :

    
        ListView{
            anchors.fill: parent
            id: schemelist
    
            model: XmlListModel{
                id: xmlListModel
                query: "/Output/user"
                XmlRole{
                    name : "name" ;
                    query: "name/string()";
                }
                XmlRole{
                    name : "surname" ;
                    query: "surname/string()";
                }
                XmlRole{
                    name : "age" ;
                    query: "age/string()";
                }
            }
    
            delegate:Item {
                     //I need to find the way to push the values into the arrays as the are processed
                    //eg
                    function storedata(){
                           names.push(name);
                           surnames.push(surname);
                           ages.push(ages)
                   }
                }
        }
    
    

    I need to store the data read from the xml to my arrays as it's processed from the ListView. Please help.



  • Check XmlListModel



  • @VRonin i did check that hance i used it, the problem is delegating the Xmllist model to a function that will store the string data as its being processed.



  • eg.

    
        ListView{
            anchors.fill: parent
            id: schemelist
    
            model: XmlListModel{
                id: xmlListModel
                query: "/GetQuote_pinnafrica_Output/Get_Quote_Response"
                XmlRole{
                    name : "Scheme_Code" ;
                    query: "Scheme_Code/string()";
                }
                XmlRole{
                    name : "Scheme_Desciption" ;
                    query: "Scheme_Description/string()";
                }
                XmlRole{
                    name : "Gross_Premium" ;
                    query: "Gross_Premium/string()";
                }
                XmlRole{
                    name : "Premium_Type" ;
                    query: "Premium_Type/string()";
                }
                XmlRole{
                    name : "Quote_Number" ;
                    query: "Quote_Number/string()";
                }
    
            }
    
            delegate:Item {
                       function storevalues(){
                               names.push(names)
                               surnames.push(surname)
                               ages.push(age)
                     }
                }
        }
    
    


  • Personally I'd do this in C++ but if you want to stick to declarative check the add() signal



  • Thanks for the reply @VRonin . I decide to take your advice on using c++ to do all the array work.

    i created a object class

    MainObject.h

    
    #ifndef MAINOBJECT_H
    #define MAINOBJECT_H
    
    #include <QObject>
    #include <QDebug>
    
    class MainObject : public QObject{
       Q_OBJECT
    public:
        explicit MainObject (QObject* parent = 0) : QObject(parent) {}
        Q_INVOKABLE void addvalues(QString name,QString surname,QString age)
        {
            qDebug() << "name      : " << name;
            qDebug() << "surname   : " << surname;
            qDebug() << "age       : " << age;
        }
    };
    
    #endif // MAINOBJECT_H
    
    

    Registered it in my main.cpp file

    qmlRegisterType<MainObject>("com.mycode", 1, 0, "MainObject");
    

    And called it in my .qml file

    
    import com.mycode 1.0
    .
    .
    .
    .
    
    MainObject.addvalues(name,surname,age);
    
    

    but now i am getting this error :

    qrc:/test.qml:69: TypeError: Property 'addvalues' of object [object Object] is not a function
    

    Is there something i am missing, Please help



  • I actually meant implementing the model as QAbstractItemModel in C++ and use it instead of the XmlListModel.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.