[Twisted-Python] Help using XmlStreamFactory & XmlStream : XPathQuery error?
Gabriel Rossetti
mailing_lists at evotex.ch
Wed Feb 20 04:36:19 EST 2008
Gabriel Rossetti wrote:
> Gabriel Rossetti wrote:
>> Hello,
>>
>> I am trying to write a Server and Client that use an XML protocol. I
>> saw that there is a XmlStream class and tried to use it, but was
>> unable to. I saw one small example for a client
>> (http://ralphm.net/blog/2005/10), but it raises some questions.
>>
>> First of all, it seams to work compleatly differently that the other
>> server/client examples.
>>
>> I don't understand why there is a need to add observers, and what
>> they are used for. I can't get any of these to work (on either side,
>> client or server):
>>
>> def connectionMade(self):
>> print 'Connected!'
>>
>> def dataReceived(self, data):
>> print data
>>
>> def onDocumentStart(self, rootElement):
>> print "Message start : ", rootElement
>>
>> def onElement(self, element):
>> print "current element : ", element
>>
>> def onDocumentEnd(self, rootElement):
>> print "Message end : ", rootElement
>>
>> I'm kind of lost :-(
>> I'm new to Twisted and I though that I understood how it works (read
>> howtos, turorials, etc, but this one doesn't seam like the others.
>> Can someone please point me to an example, I searched google but no
>> luck. All I need is a simple client-server xml echo example or a
>> better understanding of how it works and how it differs from the
>> other echo examples. Thank you,
>>
>> Gabriel
>>
>> Thank you
>>
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>
> Ok, I've got some code sort of working, if I use the following
> XPathQuery string : "/*" on the following XML string :
>
> <?xml version="1.0" encoding="utf-8"?>
> <test><feed name="test">toto</feed></test>
>
> the addObserver works ( xs.addObserver("/*", self.onTest) )
> but if I try the following :
>
>
> xs.addObserver("/test/feed/@name", self.onTest)
> or
> xs.addObserver(xpath.XPathQuery("/test/feed/@name"), self.onTest)
>
> I get the following :
>
> <......>
> File
> "/usr/lib/python2.5/site-packages/twisted/words/xish/xmlstream.py",
> line 104, in onElement
> self.dispatch(element)
> File
> "/usr/lib/python2.5/site-packages/twisted/words/xish/utility.py", line
> 232, in dispatch
> if query.matches(object):
> File "/usr/lib/python2.5/site-packages/twisted/words/xish/xpath.py",
> line 238, in matches
> return self.baseLocation.matches(elem)
> exceptions.AttributeError: 'NoneType' object has no attribute 'matches'
>
> Does anyone understand why?
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
I'm having some trouble with the XPath I think when trying to react on
xml messages. I am feeding the twisted.words.xish.xmlstream.XmlStream
the following :
<?xml version="1.0" encoding="utf-8"?>
<test><feed name='monNom'>toto</feed></test>
and the callback functions are :
def onTest(self, element):
print "got test : ", element
def onFeed(self, element):
print "got feed : ", element
def onFeedFilteredByName(self, element):
print "got feed name : ", element
def onWildcard(self, element):
print "got wildcard : ", element
and the bootstrap is added like so :
f.addBootstrap(xmlstream.STREAM_START_EVENT, connected)
and the bootstrap callback is :
def connected(self, xs):
print 'Connected!'
xs.addObserver("/test", self.onTest)
xs.addObserver("/test/feed", self.onFeed)
xs.addObserver("/test/feed[@name]", self.onFeedFilteredByName)
xs.addObserver("/*", self.onWildcard)
The only one that gets called is "/*". I tested out the others using
XPathQuery manually with :
xpath.XPathQuery("//feed[@name]").queryForNodes(root)[0]
root being the above xml, and they work, I can even get the attributes
(which is what I really need to get) like so :
xpath.XPathQuery("//feed[@name]").queryForNodes(root)[0].getAttribute("name")
What am I doing wrong when adding observers? How can I get them to react
on attribute names too?
Thank you,
Gabriel
More information about the Twisted-Python
mailing list