-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?

2

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

2 Comments

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'.
Check your xml text. From not same diferent from file. Both is stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.