[PyKDE] brain-deadness
tech at bishop.dhs.org
tech at bishop.dhs.org
Mon Jan 6 04:45:01 GMT 2003
Working through the examples in Boudewijn's book, I've come across
something. In chap. 7, it discussing using signals and slots to create
a little xml parser, which then displays in a tree list view. It just
so happens that my next project is using xml and pyqt, so this
interested me. However, (and I must be even more braindead than usual
to not understand this) how do I associate the "data" with the "tag"?
For instance, when you have a contruct like this: <tag>this is
data</tag>, the unknown_starttag signal is fired when it reaches <tag>,
and the handle_data signal is fired when it gets to "this is data", but
I'm not getting how you would say that "when unknown_starttag fires on
tag == "tag", then next handle_data should be directed to foo, which is
what I want/need.
Example code is attached (direct from the book!), along with example xml
file (and you might see what my project is ;-). I don't need working
code in response, just a general idea of what to do. Oh, and "get more
sleep, idiot" would be valid, but non-helpful B-)
Thanks,
D.A.Bishop
-------------- next part --------------
import sys
import xmllib
from qt import *
TRUE=1
FALSE=0
class Parser(xmllib.XMLParser):
def __init__(self, qObject, *args):
xmllib.XMLParser.__init__(self)
self.qObject=qObject
def start(self, document):
xmllib.XMLParser.feed(self, document)
xmllib.XMLParser.close(self)
def handle_xml(self, encoding, standalone):
self.qObject.emit(PYSIGNAL("sigXML"), (encoding, standalone))
def handle_data(self, data):
self.qObject.emit(PYSIGNAL("sigData"),(data,))
def unknown_starttag(self,tag, attributes):
self.qObject.emit(PYSIGNAL("sigStartTag"), (tag,attributes))
def unknown_endtag(self, tag):
self.qObject.emit(PYSIGNAL("sigEndTag"), (tag,))
class TreeView(QListView):
def __init__(self, *args):
apply(QListView.__init__,(self, ) + args)
self.stack=[]
self.setRootIsDecorated(TRUE)
self.addColumn("Element")
def startDocument(self, tag, attributes ):
i=QListViewItem(self)
if tag == None: tag = "None"
i.setText(0, tag)
self.stack.append(i)
def startElement(self, tag, attributes):
if tag == None: tag = "None"
i=QListViewItem(self.stack[-1])
i.setText(0,tag)
self.stack.append(i)
def Data(self, data):
print "",
def endElement(self, tag):
del(self.stack[-1])
def main(args):
if (len(args) == 2):
app=QApplication(sys.argv)
QObject.connect(app, SIGNAL('lastWindowClosed()'), app, SLOT('quit()'))
w = TreeView()
app.setMainWidget(w)
o=QObject()
p=Parser(o)
QObject.connect(o, PYSIGNAL("sigXML"),w.startDocument)
QObject.connect(o, PYSIGNAL("sigStartTag"), w.startElement)
QObject.connect(o, PYSIGNAL("sigData"), w.Data)
QObject.connect(o, PYSIGNAL("sigEndTag"), w.endElement)
s=open(args[1]).read()
p.start(s)
w.show()
app.exec_loop()
else:
print "Usage: python qtparser.py FILE.xml"
if __name__=="__main__":
main(sys.argv)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.xml
Type: text/xml
Size: 1369 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20030106/ad63623c/test.xml
More information about the PyQt
mailing list