0

Let's say we have the following xml string

xml_string = '''
<Wikimedia>
  <projects>
    <project name="Wikipedia" launch="2001-01-05">
      <editions>
        <edition language="English">en.wikipedia.org</edition>
        <edition language="German">de.wikipedia.org</edition>
        <edition language="French">fr.wikipedia.org</edition>
        <edition language="Polish">pl.wikipedia.org</edition>
        <edition language="Spanish">es.wikipedia.org</edition>
      </editions>
    </project>
    <project name="Wiktionary" launch="2002-12-12">
      <editions>
        <edition language="English">en.wiktionary.org</edition>
        <edition language="French">fr.wiktionary.org</edition>
        <edition language="Vietnamese">vi.wiktionary.org</edition>
        <edition language="Turkish">tr.wiktionary.org</edition>
        <edition language="Spanish">es.wiktionary.org</edition>
      </editions>
    </project>
  </projects>
</Wikimedia>
'''

in an ElementTree

tree = ET.ElementTree(ET.fromstring(xml_string))
root = tree.getroot()

and I am trying to get its elemens using the full path search like

root.findall('Wikimedia/projects/project/editions/edition')

but this returns an empty list [].

How do I use the full path, including the starting node Wikimedia, to do this?

1 Answer 1

2

You are calling findall() on root, which is the Wikimedia element. It works if you simply remove Wikimedia/ from the path:

ed = root.findall('projects/project/editions/edition')

In addition, the code that creates the root object can be simplified. Just use

root = ET.fromstring(xml_string)
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.