1

I use this code to make plugin in QGIS, i try to open xml from local drive, read it and then parse it and show specific information in QLine, this code i use:

from PyQt4 import QtCore, QtGui
from ui_testparse import Ui_testparse
import xml.etree.ElementTree as ETree
# create the dialog for zoom to point


class testparseDialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        # Set up the user interface from Designer.
        self.ui = Ui_testparse()
        self.ui.setupUi(self)

        opendata = self.ui.btnCari
        QtCore.QObject.connect(opendata, QtCore.SIGNAL('clicked()'),self.openxml)

    def openxml(self, event=None):

        #open dialog
        openfile = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '*.xml')

        self.ui.lineLokasi.setText(openfile)

        #call XML data
        self.isiData(openfile)

    def isiData(self, nmsatu):
        #open teks with read mode
        openteks = open(nmsatu, 'r').read()

        self.ui.textXml.setText(openteks)

        #Parse XML from Above
        self.parsenow(openteks)

    def parsenow(self, parse):
        element = ETree.fromstring(parse)
        xml_obj = ETree.ElementTree(element)
        for title_obj in xml_obj.findall('.//{http://www.isotc211.org/2005/gmd}dateStamp/'
             '{http://www.isotc211.org/2005/gco}Date'):
            print element
        self.ui.lineSkala.setText(element)

But when i try to run it i get a error message says

Traceback (most recent call last):
  File "C:\Users\Mr.Pakde/.qgis2/python/plugins\testparse\testparsedialog.py", line 47, in openxml
    self.isiData(openfile)
  File "C:\Users\Mr.Pakde/.qgis2/python/plugins\testparse\testparsedialog.py", line 56, in isiData
    self.parsenow(openteks)
  File "C:\Users\Mr.Pakde/.qgis2/python/plugins\testparse\testparsedialog.py", line 64, in parsenow
    self.ui.lineSkala.setText(element)
TypeError: QLineEdit.setText(QString): argument 1 has unexpected type 'Element'

Python version:
2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)]

Can Someone help me solve this?

1 Answer 1

1

element variable is of type Element, while setText() in QLineEdit requires a QString argument. When you:

print element

just before callin setText(), it is probably printing the string returned by the0 __repr__ method in the Element class

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.