Проблема XmlListModel при получении из API контактов Google XML - PullRequest
1 голос
/ 23 января 2020

Я пытаюсь отобразить записи из API контактов Google XML data https://developers.google.com/contacts/v3#retrieving_all_contacts со следующим кодом:

import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.XmlListModel 2.0

Item {
    width: 400
    height: 300

    ListView {
        id: listView
        anchors.fill: parent
        model: model
        delegate: Text {
            id: name
            text: qsTr("title: ") + title
        }
    }

    XmlListModel {
        id: model
        xml: "<feed"
             +" xmlns=\"http://www.w3.org/2005/Atom\""
             +" xmlns:openSearch=\"http://a9.com/-/spec/opensearch/1.1/\""
             +" xmlns:gContact=\"http://schemas.google.com/contact/2008\""
             +" xmlns:batch=\"http://schemas.google.com/gdata/batch\""
             +" xmlns:gd=\"http://schemas.google.com/g/2005\""
             +" gd:etag=\"feedEtag\">"

        +"<id>userEmail</id>"
        +"<updated>2008-12-10T10:04:15.446Z</updated>"
        +"<category scheme=\"http://schemas.google.com/g/2005#kind\" term=\"http://schemas.google.com/contact/2008#contact\"/>"
        +"<link rel=\"http://schemas.google.com/g/2005#feed\" type=\"application/atom+xml\" href=\"https://www.google.com/m8/feeds/contacts/userEmail/full\"/>"
        +"<link rel=\"http://schemas.google.com/g/2005#post\" type=\"application/atom+xml\" href=\"https://www.google.com/m8/feeds/contacts/userEmail/full\"/>"
        +"<link rel=\"http://schemas.google.com/g/2005#batch\" type=\"application/atom+xml\" href=\"https://www.google.com/m8/feeds/contacts/userEmail/full/batch\"/>"
        +"<link rel=\"self\" type=\"application/atom+xml\" href=\"https://www.google.com/m8/feeds/contacts/userEmail/full?max-results=25\"/>"
        +"<author>"
        +"<name>User</name>"
        +"<email>userEmail</email>"
        +"</author>"
        +"<generator version=\"1.0\" uri=\"http://www.google.com/m8/feeds\">Contacts</generator>"
        +"<openSearch:totalResults>1</openSearch:totalResults>"
        +"<openSearch:startIndex>1</openSearch:startIndex>"
        +"<openSearch:itemsPerPage>25</openSearch:itemsPerPage>"

        +"<entry gd:etag=\"contactEtag\">"
        +"<id>http://www.google.com/m8/feeds/contacts/userEmail/base/contactId</id>"
        +"<updated>2008-12-10T04:45:03.331Z</updated>"
        +"<app:edited xmlns:app=\"http://www.w3.org/2007/app\">2008-12-10T04:45:03.331Z</app:edited>"
        +"<category scheme=\"http://schemas.google.com/g/2005#kind\" term=\"http://schemas.google.com/contact/2008#contact\"/>"
        +"<title>Fitzwilliam Darcy</title>"
        +"<gd:name><gd:fullName>Fitzwilliam Darcy</gd:fullName></gd:name>"
        +"<link rel=\"http://schemas.google.com/contacts/2008/rel#photo\" type=\"image/*\" href=\"https://www.google.com/m8/feeds/photos/media/userEmail/contactId\" gd:etag=\"photoEtag\"/>"
        +"<link rel=\"self\" type=\"application/atom+xml\" href=\"https://www.google.com/m8/feeds/contacts/userEmail/full/contactId\"/>"
        +"<link rel=\"edit\" type=\"application/atom+xml\" href=\"https://www.google.com/m8/feeds/contacts/userEmail/full/contactId\"/>"
        +"<gd:phoneNumber rel=\"http://schemas.google.com/g/2005#home\" primary=\"true\">456</gd:phoneNumber>"
        +"<gd:extendedProperty name=\"pet\" value=\"hamster\"/>"
        +"<gContact:groupMembershipInfo deleted=\"false\" href=\"http://www.google.com/m8/feeds/groups/userEmail/base/groupId\"/>"
        +"</entry>"

        +"<!-- Other entries ... -->"

        +"</feed>"

        query: "/feed/entry"
        XmlRole { name: "title"; query: "title/string()" }

        onStatusChanged: {
            if (status == XmlListModel.Null) {
                console.log("Null");
            }
            if (status == XmlListModel.Ready) {
                console.log("Ready");
                console.log(count);
            }
            if (status == XmlListModel.Loading) {
                console.log("Loading");
            }
            if (status == XmlListModel.Error) {
                console.log("Error");
                console.log(errorString());
            }
        }
    }
}

Но, похоже, не работает и нет ошибок в журнале. Я не знаю, если это из-за схемы, так как с простыми примерами все в порядке.

Любая помощь? :)

1 Ответ

2 голосов
/ 23 января 2020

Вы должны объявить пространство имен, как указано документами :

В вашем случае:

XmlListModel {
    id: model
    // ...
    query: "/feed/entry"
    XmlRole { name: "title"; query: "title/string()" }
    <b>namespaceDeclarations: "declare default element namespace 'http://www.w3.org/2005/Atom';"</b>
    // ...
...