Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82594

Re: xml SAX Parsing in python

Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'heavily': 0.04; 'value,': 0.04; '(even': 0.05; 'handler': 0.05; 'root': 0.05; 'tree': 0.05; '#if': 0.07; 'element': 0.07; 'none:': 0.07; 'parser': 0.07; '__name__': 0.09; 'formatting': 0.09; 'parsed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:xml': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '2.7': 0.14; '"0"': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'self.root': 0.16; 'tag,': 0.16; 'subject:python': 0.16; 'variable': 0.18; 'trying': 0.19; 'stefan': 0.19; 'example': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'driven': 0.24; 'parse': 0.24; 'question': 0.24; 'define': 0.26; 'shown': 0.26; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'xml': 0.29; 'code': 0.31; 'easier': 0.31; 'class': 0.32; 'text': 0.33; 'url:python': 0.33; 'plain': 0.33; 'skip:_ 10': 0.34; 'advice': 0.35; 'skip:s 30': 0.35; 'convert': 0.35; 'but': 0.35; 'next': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'somebody': 0.38; 'url:library': 0.38; 'window': 0.38; 'to:addr:python-list': 0.38; 'structure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'up,': 0.60; 'new': 0.61; 'url:3': 0.61; 'simple': 0.61; 'below.': 0.71; 'url:htm': 0.73; '10:': 0.84; 'received:87.139': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: xml SAX Parsing in python
Date Wed, 17 Dec 2014 11:34:22 +0100
References <1562125511.108039.1418797814530.JavaMail.yahoo@jws10606.mail.bf1.yahoo.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host p578baff2.dip0.t-ipconnect.de
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
In-Reply-To <1562125511.108039.1418797814530.JavaMail.yahoo@jws10606.mail.bf1.yahoo.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17005.1418812480.18130.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418812480 news.xs4all.nl 2957 [2001:888:2000:d::a6]:34026
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:82594

Show key headers only | View raw


Hi,

Abubakar Roko schrieb am 17.12.2014 um 07:30:
> 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 definedbelow. 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 tothe program that call it.  The program shown below.
> 
> import xml.saximport XMLnode as n
> 
> class XML_Handler ( xml.sax.ContentHandler):
>     def __init__(self, root):        self.root = root        self.tmp =  n.XNode()
>     def startElement(self, tag, attributes):        #if self.root != None:
>         if self.root is not None:
>             if len(self.tmp.childs) < 10:                ID = self.tmp.ID +"." + "0" + str( len(self.tmp.childs))            else:                ID = self.tmp.ID +"." + str( len(self.tmp.childs))                         self.tmp.childs.append( n.XNode(ID,tag,"",self.tmp))             
>             self.tmp= self.tmp.childs[len(self.tmp.childs)-1]        else:            print "0", tag, self.tmp.getPath()            self.root= n.XNode("0", tag,"",None)            self.tmp=self.root
>     def characters(self, content):        self.tmp.elmValue += content.strip()
>     def endElement(self, tag):        self.tmp= self.tmp.parent
> 
>     def parse(self, f):        xml.sax.parse(self,f)        return self.root
> 
> if ( __name__ == "__main__"):
>      parser = xml.sax.make_parser()     parser.setFeature(xml.sax.handler.feature_namespaces, 0)     root = None    Handler = XML_Handler(root)    parser.setContentHandler( Handler )    treRoot= parser.parse("Movies.xml")    print treRoot
> Can somebody help me answer the following questionMy Question is how do I return the parsed result through the root instance variable of  of XML_Handler classI try to do it but i always get None as answerI am using Window 7 professional and python 2.7

The formatting of your code example was heavily screwed up, please send a
plain text email next time.

My general advice is to use ElementTree instead of SAX. It's way easier to
use (even for simple tasks). Use iterparse() to get event driven
incremental parsing.

https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.iterparse

http://effbot.org/zone/element-iterparse.htm

Stefan

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: xml SAX Parsing in python Stefan Behnel <stefan_ml@behnel.de> - 2014-12-17 11:34 +0100

csiph-web