[Twisted-Python] read all the elements of xml file

Christian Simms christian.simms at gmail.com
Sat Jan 19 07:49:57 MST 2008


On Jan 19, 2008 8:38 AM, Raj kumar <k_r_a_j_kumar at yahoo.co.in> wrote:
>
>
> Hi,
>   I am using following code
>
>
> ----------------------------------------------------------------------------------------
>      content = microdom.parse(xmlfile)
>      allnodes = content.getElementsByTagName("*")
>
>      for eachnode in allnodes
>          print "each node %s"  % eachnode
>
> ---------------------------------------------------------------------------------------
>
>  But getElement("*") is not working... which I used to get all the element
> but it is not working. If i give any tagname then it is working.
>  how to get all the elemets with single statement. Because i have different
> elements in that xml file. Is there any other way to get all the elements?
>  Am i doing anything wrong in that?
>
>  Thank you in advance.
>  ________________________________
>  5, 50, 500, 5000 - Store N number of mails in your inbox. Click here.
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>

Your code doesn't work because microdom is a simple API and doesn't
have all the bells and whistles of complex DOM implementations.  That
being said, I like it and use it.  In fact, the code is simple enough
you can just look at it to see what functions are available - just
look at twisted/web/microdom.py and twisted/web/domhelpers.py.
Anyway, for your example you can do:

   from twisted.web import domhelpers
   allnodes = domhelpers.findElements(content, lambda n: True)

The second parameter is a function to filter matching elements, in
this case the lambda function is returning all elements.

Cheers,
Christian




More information about the Twisted-Python mailing list