Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82592
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <abroko@yahoo.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'value,': 0.04; 'handler': 0.05; 'root': 0.05; 'tree': 0.05; '#if': 0.07; 'element': 0.07; 'none:': 0.07; 'parser': 0.07; '__name__': 0.09; 'parsed': 0.09; 'subject:xml': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '2.7': 0.14; '"0"': 0.16; 'received:66.196': 0.16; 'self.root': 0.16; 'tag,': 0.16; 'subject:python': 0.16; 'variable': 0.18; 'trying': 0.19; '<': 0.19; '8bit%:5': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'parse': 0.24; 'question': 0.24; 'define': 0.26; 'shown': 0.26; 'defined': 0.27; 'xml': 0.29; 'class': 0.32; 'text': 0.33; 'skip:_ 10': 0.34; 'received:66': 0.35; 'skip:s 30': 0.35; 'convert': 0.35; 'but': 0.35; '8bit%:17': 0.36; '8bit%:9': 0.36; 'thank': 0.38; '8bit%:4': 0.38; 'somebody': 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; 'skip:x 10': 0.40; 'how': 0.40; '8bit%:29': 0.60; 'received:bf1.yahoo.com': 0.60; 'received:mail.bf1.yahoo.com': 0.60; 'skip:n 30': 0.60; 'skip:\xc2 10': 0.60; 'new': 0.61; '8bit%:10': 0.64; 'header:Reply-To:1': 0.67; '8bit%:31': 0.68; 'received:98.139.212': 0.68; 'below.': 0.71; '8bit%:27': 0.74; 'received:bullet.mail.bf1.yahoo.com': 0.74; '\xc2\xa0\xc2\xa0': 0.74; '10:': 0.84; '8bit%:24': 0.84; 'received:98.139.213': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1418797815; bh=XiJUKc8dlQhpYh35OFrJ3w36GxMozKtT7nbGaSUTfgY=; h=Date:From:Reply-To:To:Subject:From:Subject; b=S7vl2hxir/o70gpgOOvRpAWOrbLDoKa33KDbFq/RGKciWDjETSGBWAlqWKQnm+4uPrGNNLNktzrJsm/5hPElYTEgSQpv4KvuCRgwMlzOkbCfkCDnhLTu+Kk0fOHrfyhRPgzybV00Giljf2MekT76xYc1Cx7/SiQDod0BifFcIp9ylyIuc2fOcFy8YgMtTX/r46SIskwJjLC+4u7H9JHG+G52Ng25GyhAclTEkXKdJx+t6GKisPPBg4uCQCA7yPw+VMqFEIOCMuzKspOWWgWkUB0NcrCI6XCXD41sWtEMg3WyLBVTeGqL/ogF51CzFvDh4c5NvXr8/oLsALaR44+bbg== |
| DomainKey-Signature | a=rsa-sha1; q=dns; c=nofws; s=s2048; d=yahoo.com; b=WAEe3MtqGlgtBVFIsjNVpO3zqkMFZ8pBWMMrd10apkL7Fm42fbKdAjVJGiw1UFZtOuqGGgZWs99iRdQqVYjEWtVqdFvRyVipijnTGnIKSYlY/hkkgEgW5/RKXjuKoBTkZsl94HWE3VEkccLuZnAxQG4gK3Yf3mLpL+403V/9Qbuvb/b1cJ9En0U8QAegd6S4SCOejtzT8ImE36QwbTM6czEHPyzwZ31P3hShr138jov0aWI+Q5MWf999N5B5vngbWXlBCRMgkPpqez/UtRiZhR1OIUpTJ/aiaP+e+X4Ebofm+381C6uRyT/xZuCWm7/JofRqOS5Hovc/A/3eUvPDaw==; |
| X-Yahoo-Newman-Property | ymail-3 |
| X-Yahoo-Newman-Id | 281207.40905.bm@omp1027.mail.bf1.yahoo.com |
| X-YMail-OSG | 5jCtmYQVM1nxvffDB20DpouFbJ4nUDF_krNDp_.TzAFs3at1L1PaLtkKrMHCZFY l1NfesvfXSb8KOPdAIwGMrmv980jiDh2iGROh.brGODkqxyY27zATZ3R6UBDXOIjdn1FvWShNNm1 Vxg12fEgMWTJmeVKexraJB.LQitE6xfLbi9B6lN.WFvOr2QsfbHzV_F.2le1OchIW5yFsmWf79QL InDPsWdmTU07A4T7YOxMVDnAjqshQmYwPW.0zAmzxfFLwFyWAYZc9kBOOCZSYcG_QBvvcgR4dLi_ ZNouUyi_Glbik0q_qsNjP3J5i.n0jPI_ETlMxJe33xMoLZfbDxwWZNcdm9NczfZq2w96Y7yRzq29 glzH1VtlnnQYYvkkwgUyBDhgIE.zJVw_3VOtPZoOOyPxELX24C2b8Tec_hmBvkTwWcftCsIAiWAi g6nTVY7NbOTB7Bgt3F6OGIXudwiwqUqiEPFSwjFwCR2dfGUp1k5CgyKizMaMjuSFFOs2jdiCOwmb puKlYpyxOO6TMMQfM40al0K5QzTDiEonRNwOlvpUMOmzuaI6EJXetdk9f9omjF6jH_I0QKw-- |
| Date | Wed, 17 Dec 2014 06:30:14 +0000 (UTC) |
| From | Abubakar Roko <abroko@yahoo.com> |
| To | "python-list@python.org" <python-list@python.org> |
| Subject | xml SAX Parsing in python |
| MIME-Version | 1.0 |
| Content-Type | multipart/alternative; boundary="----=_Part_108038_1614216160.1418797814517" |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| Reply-To | Abubakar Roko <abroko@yahoo.com> |
| 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.17003.1418797955.18130.python-list@python.org> (permalink) |
| Lines | 269 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1418797955 news.xs4all.nl 2924 [2001:888:2000:d::a6]:45456 |
| X-Complaints-To | abuse@xs4all.nl |
| X-Received-Body-CRC | 314341215 |
| X-Received-Bytes | 23833 |
| Xref | csiph.com comp.lang.python:82592 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
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 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
Thank you
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
xml SAX Parsing in python Abubakar Roko <abroko@yahoo.com> - 2014-12-17 06:30 +0000
csiph-web