-1

I have the following XML:

<part>
 <part_id>151</part_id>
 <part_name>BBa_B0034</part_name>
 <part_short_name>B0034</part_short_name>
 <part_short_desc>RBS (Elowitz 1999) -- defines RBS efficiency</part_short_desc>
 <part_type>RBS</part_type>
 <release_status>Released HQ 2013</release_status>
 <sample_status>In stock</sample_status>
 <part_results>Works</part_results>
 <part_nickname/>
 <part_rating>1</part_rating>
 <part_url>http://parts.igem.org/Part:BBa_B0034</part_url>
 <part_entered>2003-01-31</part_entered>

And I want to extract some of the values.

For example I want to ouput the the BBa_B0034 from <part_name>.

How would I do this using ElementTree?

Charon
  • 2,344
  • 6
  • 25
  • 44
  • 1
    What have you tried? Have you looked at the [documentation for `ElementTree`](http://docs.python.org/3.3/library/xml.etree.elementtree.html)? Try to work with examples and write code and then post what you got here when things don't work the way you expect. – metatoaster Mar 13 '14 at 11:12
  • Duplicates :http://stackoverflow.com/questions/1786476/parsing-xml-in-python-using-elementtree-example?rq=1 – Laxmikant Ratnaparkhi Mar 13 '14 at 11:13

1 Answers1

0

You need correct xml for parser correct. For reference using Xml.etree Python.

See example:

>>> import xml.etree.ElementTree as ET
>>> tree = ET.fromstring('<part> <part_id>151</part_id> <part_name>BBa_B0034</part_name>     <part_short_name>B0034</part_short_name> <part_short_desc>RBS (Elowitz 1999) -- defines RBSefficiency</part_short_desc> <part_type>RBS</part_type> <release_status>Released HQ 2013</release_status> <sample_status>In stock</sample_status> <part_results>Works</part_results> <part_nickname/> <part_rating>1</part_rating> <part_url>http://parts.igem.org/Part:BBa_B0034</part_url> <part_entered>2003-01-31</part_entered></part>')
>>> tree.findall('./part_name')[0].text
'BBa_B0034'
Jones
  • 1,480
  • 19
  • 34
  • Thanks for this but when I try and use that I get 'list index out of range' - I am parsing an xml from the web, as opposed to using 'fromstring'. – Charon Mar 13 '14 at 11:38
  • Check your xml text. From not same diferent from file. Both is stream. – Jones Mar 16 '14 at 02:36