3

In my project I need to parse an XML document using lxml.etree. I'm novice in Python, so I can't understand, how to find all categories with some tag. Let's describe it more accurately.

I have an XML like:

<cinema>
  <name>BestCinema</name>
  <films>
    <categories>
      <category>Action</category>
      <category>Thriller</category>
      <category>Soap opera</category>
    </categories>
  </films>
</cinema>

Now I need to get the list of all categories. In this case it will be:

      <category>Action</category>
      <category>Thriller</category>
      <category>Soap opera</category>

I need to use:

tree = etree.parse(file)

Thank you, any help is welcome.

1 Answer 1

3

it should be as simple as:

from lxml import etree
el = etree.parse('input.xml')
categories = el.xpath("//category")
print(categories)
...

Everything else you should find in the tutorial.

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

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.