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
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)
<?xml version='1.0' encoding="ISO-8859-1"?>
  <PACKAGE>
    <TITLE>Treasures</TITLE>
    <ACTION>download</ACTION>
    <TARGET>Treasures</TARGET>
    <EXP_DATE>7/4/2003 17:29</EXP_DATE>

    <PROVIDER>
      <AUTHOR>EMusic.com</AUTHOR>
      <NAME>EMusic.com</NAME>
      <URL>http://www.emusic.com</URL>
      <COPYRIGHT>Portions Copyright (C) 2002 EMusic.com Inc; see http://www.emusic.com for more details</COPYRIGHT>
      <CONTACT>[EMAIL PROTECTED]</CONTACT>
    </PROVIDER>

    <SERVER>
      <NAME>EMusic.com</NAME>
      <DESC>Emusic content server</DESC>
      <NETNAME>downloads.mp3.com</NETNAME>
      <LOCATION>/%fid/%f</LOCATION>
      <KEY>Not used</KEY>
    </SERVER>

    <TRACKLIST>
    <LISTID>0</LISTID>

      <TRACK>
        <TRACKID>AFqUBgAARw7GNy1iHSoAAF7zBAAAXfMEAABCX8uvAEPazRg.AgBcUQAAAAGAQSQMGRzAA0JMVEyQdgejHFuDoHBXZqcF1ZE-</TRACKID>
        <TRACKNUM>1</TRACKNUM>
        <TITLE>Treasures</TITLE>
        <ALBUM>The Mirror Conspiracy</ALBUM>
        <ARTIST>Thievery Corporation</ARTIST>
        <GENRE>Electronic</GENRE>
        <FILENAME>Treasures.mp3</FILENAME>
        <FORMAT>.mp3</FORMAT>
        <QUALITY>128000</QUALITY>
        <CHANNELS>2</CHANNELS>
        <DURATION>144</DURATION>
        <ALBUMART>http://images.mp3.com/emusic/goodnoise/emusic_generic_cover_80x80.jpg</ALBUMART>
      </TRACK>

    </TRACKLIST>
  </PACKAGE>

Reply via email to