xml SAX Parsing in python

Ian Kelly ian.g.kelly at gmail.com
Wed Dec 17 11:39:28 EST 2014


On Tue, Dec 16, 2014 at 11:30 PM, Abubakar Roko
<abroko at yahoo.com.dmarc.invalid> wrote:
>
> Good day,
>
> Please I am new in using python to write program. I am trying to parse an
XML document using sax parse  and store the parsed result in a tree like
defined
> below. XNode class define an xml element which has an ID , a tag, a text
value, children element and a parent element
>
>                    class XNode(object):
>
>                             def __init__(self, ID ="", elmName="",
elmValue="", parent=None):
>
>                                  self.ID = ID
>                                  self.elmName=elmName
>                                  self.elmValue=elmValue
>                                  self.childs=[]
>                                 self.parent=parent
>
>
>                            def  getPath(self):
>                                   if self.parent is None:
>                                        return self.elmName
>                                  else:
>                                        return self.parent.getPath()+"/"+
self.elmName
>
> I  wrote a program that parse an XML document ,  convert the  document
 into the tree like structure defined above and then return the parsed
result to
> the program that call it.  The program shown below.

I'm not sure why you would want to use a SAX parser for this. The advantage
of incremental parsing is that you never have to have the whole document in
memory at once. If you use it to construct some DOM-like structure from the
document, then you're going to have the whole document in memory anyway,
and you might as well just use one of the existing implementations (e.g.
ElementTree) rather than reinventing your own.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141217/ccd91a37/attachment.html>


More information about the Python-list mailing list