Re: python xml (Lukasz Szybalski)

"David Webster" <[email protected]>
Newsgroups gmane.comp.python.xml
Message-ID <78B44E1448B1EC49888FF6DEDD72FF841A601F@STAFEXVS04.ioinc.ioroot.tld>
Look at BeautifulSoup:
http://www.crummy.com/software/BeautifulSoup/
The BeautifulSOAP class may do what you need.

> Date: Tue, 07 Mar 2006 11:23:55 -0600
> From: Lukasz Szybalski <[email protected]>
> Subject: [XML-SIG] python xml
> To: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> Hello,
> I am new to xml in python, but not new to python. After searching for 
> quite a bit, I can't seem to find the answer to this easy 
> coding problem.
> I have a xml file that i need to find an element with id = 2 extract 
> second child from it, and pass it on to the rest of my program.
> 
> How can i do it in "<10" lines of code in python?
> 
> --sample-File------default.xml--
> <?xml version="1.0"?>
> <response id="1">
>     <category>out</category>
>         <text>Hello,
>                 I'm out of office...etc..
>         </text>
> </response>
> <response id="2">
>     <category>in</category>
>         <text>Hello,
>                 I'm in the office...etc..
>         </text>
> </response>
> 
> 
> ---python --
> # I found most of the info/how to on minidom, so I assume 
> minidom is the 
> best choice here
> 
> from xml.dom import minidom, Node
> #parse a file,
> doc=minidom.parse('default.xml')
> # find attribute
> 
> .....and here I find myself reading tons of how to, but can't seem to 
> find what i'm looking for. .
> 
> #how can i do something like: (please correct me here)
>  for x in  doc.nodeList:
>     if x.attribute.get('id')==2:
>        pass_it_on = ????      #How do I get to <text> and 
> extract it as 
> string?
> 
> ---------EOF------
> Thanks for the help
> Lucas
> 
_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.