XML parsing per record

Kent Johnson kent37 at tds.net
Sat Apr 23 12:16:23 EDT 2005


Willem Ligtenberg wrote:
> Is there an easy way, to couple data together. Because I have discoverd an
> irritating feature in the xml file.
> Sometimes this is a database reference:
> <Dbtag>
> 	<Dbtag_db>UCSC</Dbtag_db>
> 	<Dbtag_tag>
> 		<Object-id>
> 			<Object-id_str>1234</Object-id_str>
> 		</Object-id>
> 	</Dbtag_tag>
> </Dbtag>
> 
> And sometimes:
> 
> <Dbtag>
> 	<Dbtag_db>UCSC</Dbtag_db>
> 	<Dbtag_tag>
> 		<Object-id>
> 			<Object-id_id>1234</Object-id_id>
> 		</Object-id>
> 	</Dbtag_tag>
> </Dbtag>
> 
> So I get a list database names and two! lists of ID's
> And those two are in no way related. Is there an easy way to create a
> dictionary like this DBname --> ID
> If not, I still might need to revert to SAX... :(

None of your requirements sound particularly difficult to implement. If you would post a complete 
example of the data you want to parse and the data you would like to end up it would be easier to 
help you. The sample data you posted originally does not have many of the fields you want to extract 
and your example of what you want to end up with is not too clear either.

If you are having trouble with ElementTree I expect you will be completely lost with SAX, 
ElementTree is much easier to work with and cElementTree is very fast.

Kent



More information about the Python-list mailing list