コード例 #1
0
ファイル: weather.py プロジェクト: Chetchita641/QT_Tutorials
    def handleResponse(self, reply):

        er = reply.error()

        if er == QtNetwork.QNetworkReply.NoError:

            data = reply.readAll()

            r = QXmlStreamReader(str(data, 'UTF-8'))

            while not r.atEnd():

                r.readNext()

                if (r.qualifiedName() == "yweather:location"):

                    if r.isStartElement():

                        atr = r.attributes()
                        print("City:", atr.value("city"))
                        print("Country:", atr.value("country"))

                if (r.qualifiedName() == "yweather:condition"):

                    if r.isStartElement():

                        atr = r.attributes()
                        print("Date:", atr.value("date"))
                        print("Condition:", atr.value("text"))
                        print("Temperature:", \
                            atr.value("temp"), "deg C")

            if r.hasError():
                print("Error reading feed")

        else:
            print("Error occured: ", er)
            print(er)
            print(reply.errorString())

        QCoreApplication.quit()
コード例 #2
0
    def read_xml_file(self, xml, tag):

        try:
            xml_file = QFile(os.path.join(os.curdir, 'in', xml))
            xml_file.open(xml_file.ReadOnly | xml_file.Text)
            doc = QXmlStreamReader(xml_file)
            text_list = []
            while not doc.atEnd():
                doc.readNextStartElement()
                if doc.name() == tag:
                    temp = doc.namespaceUri()
                    text_list.append(
                        '{0}, {1}, {2}'.format(doc.namespaceUri(), doc.qualifiedName(), doc.readElementText()))
            xml_file.close()
            return text_list
        finally:
            xml_file.close()