Populate a list from xml using python
I have an xml file in the following format:
<food>
<desert>
cake
<desert>
</food>
<history>
currently in my belly
</history>
I want to create two list, food and text populated with cake and history in string format. Is there an easy way to do it 开发者_StackOverflow中文版in python?
ElementTree (and the faster compatible implementations in cElementTree
and lxml
) let you very easily extract information from XML -- as long as the XML is correct and your goals are well defined.
Your example XML has at least two problems (you're opening another dessert
tag instead of closing the first one, and you're missing any root element) and your goals are pretty mysterious to me (e.g., where's "text" supposed to come from?). If you edit your Q to fix your XML and clarify your exact specifications, I'm sure we can help you more.
You could import xml.dom
and use it to create a DOM tree which you can then read like any other DOM tree
精彩评论