Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all nodes by attribute in XML using Python 2

I have an XML file which has a lot of different nodes with the same attribute.

I was wondering if it's possible to find all these nodes using Python and any additional package like minidom or ElementTree.

like image 458
midori Avatar asked Jan 07 '15 03:01

midori


2 Answers

You can use built-in xml.etree.ElementTree module.

If you want all elements that have a particular attribute regardless of the attribute values, you can use an xpath expression:

//tag[@attr]

Or, if you care about values:

//tag[@attr="value"]

Example (using findall() method):

import xml.etree.ElementTree as ET

data = """
<parent>
    <child attr="test">1</child>
    <child attr="something else">2</child>
    <child other_attr="other">3</child>
    <child>4</child>
    <child attr="test">5</child>
</parent>
"""

parent = ET.fromstring(data)
print [child.text for child in parent.findall('.//child[@attr]')]
print [child.text for child in parent.findall('.//child[@attr="test"]')]

Prints:

['1', '2', '5']
['1', '5']
like image 66
alecxe Avatar answered Sep 19 '22 00:09

alecxe


This is a good sample/start script using xpath :

# -*- coding: utf-8 -*-
from lxml import etree
fp = open("xml.xml")
tree = etree.parse(fp)
for el in tree.findall('//node[@attr="something"]'):
    print(el.text)
like image 23
Gilles Quenot Avatar answered Sep 18 '22 00:09

Gilles Quenot