2014-04-21 41 views
1

我使用此代碼在QGIS中創建插件,我嘗試從本地驅動器打開xml,讀取它然後解析它並在QLine中顯示特定信息, :參數1在使用ElementTree解析xml時有意想不到的類型錯誤

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) 

但是,當我嘗試運行它,我得到一個錯誤消息指出

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)] 

有人可以幫我解決這個問題?

回答

1

element變量是Element型的,而在setText()QLineEdit需要QString參數。當你:

print element 

剛剛呼喚的setText(),它可能是打印由the0 __repr__方法在Element類返回的字符串前

相關問題