我尝试编辑我上次在插件中使用的代码来打开和解析xml,我仍然使用Element Tree因为它集成在python中,这段代码我编辑
from PyQt4 import QtCore, QtGui
from ui_latih import Ui_latih
import xml.etree.ElementTree as ETree
# create the dialog for zoom to point
class latihDialog(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
# Set up the user interface from Designer.
self.ui = Ui_latih()
self.ui.setupUi(self)
cariButton = self.ui.btnCari
QtCore.QObject.connect(cariButton, QtCore.SIGNAL('clicked()'),self.cari)
def cari(self, event=None):
#open dialog
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '*.xml')
self.ui.lineFile.setText(filename)
#panggil isi data
self.isiDataFile(filename)
def isiDataFile(self, nmfile):
#buka dengan open mode baca
teksFile = open(nmfile, 'r').read()
self.ui.textFile.setText(teksFile)
#parse XML FIle
self.parsethis(teksFile)
def parsethis(self, parse):
element = ETree.fromstring(parse)
xml_obj = ETree.ElementTree(element)
for title_obj in xml_obj.findall('.//gco:Integer'):
self.ui.lineSkala.setText(element)
我尝试使用不同的样式更改元素树代码以查看结果
def parsethis(self, parse):
doc = ETree.fromstring(parse)
for elem in doc.findall('Default_Config/gco:Integer'):
a=elem.tag, elem.text
self.ui.lineSkala.setText(a)
但是这2个代码给了我同样的错误信息:
File "C:\PROGRA~1\QGISDU~1\apps\Python27\lib\xml\etree\ElementPath.py", line 83, in xpath_tokenizer
raise SyntaxError("prefix %r not found in prefix map" % prefix)
SyntaxError: prefix 'gco' not found in prefix map
Python version:
2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)]
有人可以帮帮我吗?