XML Parsing

hrishy hrishys at yahoo.co.uk
Wed Feb 25 01:09:25 EST 2009


Hi 

I am just a python enthusiast and not a python user but was just wundering why didnt the list members come up with or recommen XPATH based solution
which i think is very elegant for this type of a problem isnt it ?

regards
Hrishy



--- On Wed, 25/2/09, Lie Ryan <lie.1296 at gmail.com> wrote:

> From: Lie Ryan <lie.1296 at gmail.com>
> Subject: Re: XML Parsing
> To: python-list at python.org
> Date: Wednesday, 25 February, 2009, 5:43 AM
> On Tue, 24 Feb 2009 20:50:20 -0800, Girish wrote:
> 
> > Hello,
> > 
> > I have a xml file which is as follows:
> > 
> >     <pids>
> >         <Parameter_Class>
> >             <Parameter
> Id="pid_031605_093137_283">
> >                
> <Identifier>$0000</Identifier>
> >                 <Type>PID</Type>
> >                 <Signal><![CDATA[Parameter
> Identifiers Supported - $01
> > to $20]]></Signal>
> >                 <Description><![CDATA[This
> PID indicates which
> > legislated PIDs]]></Description>
> >                  ..............
> >                  ...............
> > 
> > Can anyone please tell me how to get content of
> <Signal> tag.. that is,
> > how to extract the data "![CDATA[Parameter
> Identifiers Supported - $01
> > to $20]]"
> > 
> > Thanks,
> > Girish...
> 
> The easy one is to use re module (Regular expression). 
> 
> # untested
> import re
> signal_pattern =
> re.compile('<Signal>(.*)</Signal>')
> signals = signal_pattern.findall(xmlstring)
> 
> also, you may also use the xml module, which will be more
> reliable if you 
> have data like this: <foo
> attr="<Signal>blooo</Signal>">blah</foo>,
> 
> >>> import xml.dom.minidom
> >>> xmldata =
> xml.dom.minidom.parse(open('myfile.xml'))
> >>> for node in
> xmldata.getElementsByTagName('Signal'): print
> node.toxml()
> ... 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list


      



More information about the Python-list mailing list