1

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

1
  • 1
    As a comment, dessert is spelt with two s 's, as in you'd want 2 helpings. Commented Mar 31, 2010 at 1:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You could import xml.dom and use it to create a DOM tree which you can then read like any other DOM tree

Comments

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.