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: 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 To: "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 List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 ------=_Part_108038_1614216160.1418797814517 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Good day, Please I am new in using python to write program. I am trying to parse an X= ML document using sax parse =C2=A0and store the parsed result in a tree lik= e definedbelow. XNode class define an xml element which has an ID , a tag, = a text value, children element and a parent element =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0class = XNode(object): =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 def __init__(self, ID =3D"", elmName=3D"", elmValu= e=3D"", parent=3DNone): =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.ID =3D ID=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.elmName=3DelmName=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0self.elmValue=3DelmValue=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0self.childs=3D[]=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.= parent=3Dparent =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0def =C2=A0getPath(self):=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 if self.parent is None:=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return self.elmName=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0else:=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0return self.parent.getPath()+"/"+ self.elmName I =C2=A0wrote a program that=C2=A0parse=C2=A0an XML document , =C2=A0conver= t=C2=A0the =C2=A0document =C2=A0into the tree like structure defined above = and then=C2=A0return=C2=A0the parsed result tothe program that call it. =C2= =A0The program shown below. import xml.saximport XMLnode as n class XML_Handler ( xml.sax.ContentHandler): =C2=A0 =C2=A0 def __init__(self, root):=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.roo= t =3D root=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.tmp =3D =C2=A0n.XNode() =C2=A0 =C2=A0 def startElement(self, tag, attributes):=C2=A0 =C2=A0 =C2=A0 = =C2=A0 #if self.root !=3D None: =C2=A0 =C2=A0 =C2=A0 =C2=A0 if self.root is not None: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if len(self.tmp.childs) < 10:=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ID =3D self.tmp.ID +".= " + "0" + str( len(self.tmp.childs))=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 else:=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ID =3D sel= f.tmp.ID +"." + str( len(self.tmp.childs))=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.tmp.childs.= append( n.XNode(ID,tag,"",self.tmp))=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.tmp=3D self.tmp.childs[len(s= elf.tmp.childs)-1]=C2=A0 =C2=A0 =C2=A0 =C2=A0 else:=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 print "0", tag, self.tmp.getPath()=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 self.root=3D n.XNode("0", tag,"",None)=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.tmp=3Dself.root =C2=A0 =C2=A0 def characters(self, content):=C2=A0 =C2=A0 =C2=A0 =C2=A0 sel= f.tmp.elmValue +=3D content.strip() =C2=A0 =C2=A0 def endElement(self, tag):=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.tm= p=3D self.tmp.parent =C2=A0 =C2=A0 def parse(self, f):=C2=A0 =C2=A0 =C2=A0 =C2=A0 xml.sax.parse(= self,f)=C2=A0 =C2=A0 =C2=A0 =C2=A0 return self.root if ( __name__ =3D=3D "__main__"): =C2=A0 =C2=A0 =C2=A0parser =3D xml.sax.make_parser()=C2=A0 =C2=A0 =C2=A0par= ser.setFeature(xml.sax.handler.feature_namespaces, 0)=C2=A0 =C2=A0 =C2=A0ro= ot =3D None=C2=A0 =C2=A0 Handler =3D XML_Handler(root)=C2=A0 =C2=A0 parser.= setContentHandler( Handler )=C2=A0 =C2=A0 treRoot=3D parser.parse("Movies.x= ml")=C2=A0 =C2=A0 print treRoot Can somebody help me answer the following questionMy Question is how do I r= eturn the parsed result through the=C2=A0root instance variable=C2=A0of =C2= =A0of XML_Handler classI try to do it but i always get=C2=A0None=C2=A0as an= swerI am using Window 7 professional and python 2.7 Thank you =C2=A0 ------=_Part_108038_1614216160.1418797814517 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Good day,

Please I am new in using python to write= program. I am trying to parse an XML document using sax parse  and st= ore the parsed result in a tree like defined
below. XN= ode class define an xml element which has an ID , a tag, a text value, chil= dren element and a parent element

             = ;      class XNode(object):

          &= nbsp;                 def __init__(= self, ID =3D"", elmName=3D"", elmValue=3D"", parent=3DNone):

    &nbs= p;                     &n= bsp;      self.ID =3D ID
    =                      = ;        self.elmName=3DelmName
&n= bsp;                     =            self.elmValue=3DelmValue
                  =                self.childs=3D[]
                &= nbsp;               self.parent=3Dparent=


        &nbs= p;                  def  = getPath(self):
          &nbs= p;                     &n= bsp; if self.parent is None:
      &nbs= p;                     &n= bsp;          return self.elmName
                   =              else:
                    &nbs= p;                  return sel= f.parent.getPath()+"/"+ self.elmName

I  wrote a program that parse an XML document ,  convert the  document  into the tree like structur= e defined above and then return the = parsed result to
the program that call it.  The p= rogram shown below.


import xml.sa= x
import XMLnode as n


class XML_Handler ( xml.sax.ContentHandler):

    def __init= __(self, root):
        self.root = =3D root
        self.tmp =3D &nb= sp;n.XNode()

    def startElement(self, tag, attributes):
        #if self.root !=3D None:

    &nbs= p;   if self.root is not None:

            if= len(self.tmp.childs) < 10:
      &n= bsp;         ID =3D self.tmp.ID +"." + "0" + str( len(s= elf.tmp.childs))
          &n= bsp; else:
            &= nbsp;   ID =3D self.tmp.ID +"." + str( len(self.tmp.childs))
             
            self.tmp.childs.append( = n.XNode(ID,tag,"",self.tmp))
      &nbs= p;      

            self.tmp=3D sel= f.tmp.childs[len(self.tmp.childs)-1]
    &nb= sp;   else:
          &n= bsp; print "0", tag, self.tmp.getPath()
    =         self.root=3D n.XNode("0", tag,"",None)
            self.tmp=3Dself.root=

 = ;   def characters(self, content):
    =     self.tmp.elmValue +=3D content.strip()
<= br class=3D"" style=3D"">
    def endElement= (self, tag):
        self.tmp=3D s= elf.tmp.parent


    def = parse(self, f):
        xml.sax.pa= rse(self,f)
        return self.ro= ot

if ( __name__ =3D=3D "__main__= "):

&n= bsp;    parser =3D xml.sax.make_parser()
&nb= sp;    parser.setFeature(xml.sax.handler.feature_namespaces, 0)
     root =3D None
=     Handler =3D XML_Handler(root)
  &nb= sp; parser.setContentHandler( Handler )
    = treRoot=3D parser.parse("Movies.xml")
    pr= int treRoot

Can somebody help me answer the following question
My Question is how do I return the parsed result through the ro= ot instance variable of  of XML_Handler class
I try to do it but i always get None as answer
=
I am using Window 7 professional and python 2.7

Thank you  =
------=_Part_108038_1614216160.1418797814517--