shivam mitra
shivam mitra

Reputation: 232

Iterating over children of a particular tag using elementtree

I am a beginner to elementtree concepts.

I just learnt how to iterate over all the tags in xml file.

for elem in root.iter():
    print elem.tag, elem.attrib

Now, I want to iterate over all the children of a particular tag.

Let's name the tag as "teams".

So, I want to iterate over all the children of this tag.

How can I achieve this? I am using python for parsing the xml file.

Upvotes: 0

Views: 7004

Answers (2)

har07
har07

Reputation: 89325

You can use simple XPath selector expression to get all child elements of teams :

for elem in root.findall(".//teams/*"):
    print elem.tag, elem.attrib

Upvotes: 3

WBAR
WBAR

Reputation: 4984

Let assume You have file:

example.xml:

<example>
    <sample>
        <teams>
            <team>
                <id>1</id>
                <name>example1</name>
            </team>
            <team>
                <id>2</id>
                <name>example2</name>
            </team>
            <team>
                <id>3</id>
                <name>example3</name>
            </team>
        </teams>
    </sample>
    <league>
        <amateur>
            <teams>
                <team>
                    <id>1</id>
                    <name>example1</name>
                </team>
                <team>
                    <id>2</id>
                    <name>example2</name>
                </team>
                <team>
                    <id>3</id>
                    <name>example3</name>
                </team>
            </teams>
        </amateur>
        <pro>
            <teams>
                <team>
                    <id>1</id>
                    <name>example1</name>
                </team>
                <team>
                    <id>2</id>
                    <name>example2</name>
                </team>
                <team>
                    <id>3</id>
                    <name>example3</name>
                </team>
            </teams>
        </pro>
    </league>
</example>

Minidom:

Now using Python You can do this:

from xml.dom import minidom
exampleDOM = minidom.parse('example.xml')
teams = exampleDOM.getElementsByTagName('teams')[0] # in sample section
for team in teams.getElementsByTagName('team'):
    id = team.getElementsByTagName('id')[0].childNodes[0].toxml()
    name = team.getElementsByTagName('name')[0].childNodes[0].toxml()
    print('Team (%s): %s' % (id, name))

Output:

Team (1): example1
Team (2): example2
Team (3): example3

ElementTree

Using ElementTree library You can achieve this easier:

import xml.etree.ElementTree as ET
tree = ET.parse('example.xml')
root = tree.getroot()
for teams in root.iter('teams'):
    for team in teams.iter('team'):
         id = team.find('id').text
         name = team.find('name').text
         print('Team (%s): %s' % (id, name))

Output:

Team (1): example1
Team (2): example2
Team (3): example3
Team (1): example1
Team (2): example2
Team (3): example3
Team (1): example1
Team (2): example2
Team (3): example3

Upvotes: 2

Related Questions