get all xml attribute values in python3 using elementtree

  • Last Update :
  • Techknowledgy :

Use findall() to get all of the country tags and get the name attribute from the .attrib dictionary:

import xml.etree.ElementTree as ET

data = ""
"your xml here"
""

tree = ET.fromstring(data)
print([el.attrib.get('name') for el in tree.findall('.//country')])

FYI, lxml provides a more complete xpath support and therefore makes it easier to get the attribute values:

from lxml
import etree as ET

data = ""
"your xml here"
""

tree = ET.fromstring(data)
print(tree.xpath('//country/@name'))

You could use

 import xml.etree.ElementTree as ET

 xml = ET.fromstring(contents)

 xml.find('./element').attrib['key']

Suggestion : 2

The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data.,Changed in version 3.3: This module will use a fast implementation whenever available.,Deprecated since version 3.3: The xml.etree.cElementTree module is deprecated.,The xml.etree.ElementTree module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML vulnerabilities.

<?xml version="1.0"?>
<data>
   <country name="Liechtenstein">
      <rank>1</rank>
      <year>2008</year>
      <gdppc>141100</gdppc>
      <neighbor name="Austria" direction="E" />
      <neighbor name="Switzerland" direction="W" />
   </country>
   <country name="Singapore">
      <rank>4</rank>
      <year>2011</year>
      <gdppc>59900</gdppc>
      <neighbor name="Malaysia" direction="N" />
   </country>
   <country name="Panama">
      <rank>68</rank>
      <year>2011</year>
      <gdppc>13600</gdppc>
      <neighbor name="Costa Rica" direction="W" />
      <neighbor name="Colombia" direction="E" />
   </country>
</data>
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
root = ET.fromstring(country_data_as_string)
>>> root.tag 'data' >>>
   root.attrib {}
>>>
for child in root:
   ...print(child.tag, child.attrib)
   ...
   country {
      'name': 'Liechtenstein'
   }
country {
   'name': 'Singapore'
}
country {
   'name': 'Panama'
}
>>> root[0][1].text '2008'

Suggestion : 3

Class Starts on 13th August,2022

EXAMPLE:

<?xml version="1.0" encoding="UTF-8"?>
<metadata>
   <food>
      <item name="breakfast">Idly</item>
      <price>$2.5</price>
      <description>
         Two idly's with chutney
      </description>
      <calories>553</calories>
   </food>
   <food>
      <item name="breakfast">Paper Dosa</item>
      <price>$2.7</price>
      <description>
         Plain paper dosa with chutney
      </description>
      <calories>700</calories>
   </food>
   <food>
      <item name="breakfast">Upma</item>
      <price>$3.65</price>
      <description>
         Rava upma with bajji
      </description>
      <calories>600</calories>
   </food>
   <food>
      <item name="breakfast">Bisi Bele Bath</item>
      <price>$4.50</price>
      <description>
         Bisi Bele Bath with sev
      </description>
      <calories>400</calories>
   </food>
   <food>
      <item name="breakfast">Kesari Bath</item>
      <price>$1.95</price>
      <description>
         Sweet rava with saffron
      </description>
      <calories>950</calories>
   </food>
</metadata>

EXAMPLE:

import xml.etree.ElementTree as ET
mytree = ET.parse('sample.xml')
myroot = mytree.getroot()
print(myroot)

You can also use fromstring() function to parse your string data. In case you want to do this, pass your XML as a string within triple quotes as follows:

import xml.etree.ElementTree as ET
data='''
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
   <food>
      <item name="breakfast">Idly</item>
      <price>$2.5</price>
      <description>
         Two idly's with chutney
      </description>
      <calories>553</calories>
   </food>
</metadata>
'''
myroot = ET.fromstring(data)
#print(myroot)
print(myroot.tag)


EXAMPLE:

for x in myroot.findall('food'):
   item = x.find('item').text
price = x.find('price').text
print(item, price)

EXAMPLE:

myroot[0].clear()
mytree.write('output7.xml')

Suggestion : 4

Getting child tag's attribute value in a XML using ElementTree. Parse the XML file and get the root tag and then using [0] will give us first child tag. Similarly [1], [2] gives us subsequent child tags. After getting child tag use .attrib[attribute_name] to get value of that attribute. , 1 week ago Mar 19, 2017  · Getting child tag's attribute value in a XML using ElementTree. Parse the XML file and get the root tag and then using [0] will give us first child tag. ... Tags: python xml xpath elementtree. Related. Dataframe: Remove one row per sub-set conditionally; Logging setLevel is being ignored in Python; , 6 days ago Getting child tag's attribute value in a XML using ElementTree. Parse the XML file and get the root tag and then using [0] will give us first child tag. Similarly [1], [2] gives us subsequent child tags. After getting child tag use .attrib[attribute_name] to get value of that attribute. >>> import xml.etree.ElementTree as ET >>> xmlstr = '<foo ... ,Use findall() to get all of the country tags and get the name attribute from the .attrib dictionary:


<?xml version="1.0"?> <data>
   <country name="Liechtenstein">
      <rank updated="yes">2</rank>
      <year>2008</year>
      <gdppc>141100</gdppc>
      <neighbor name="Austria" direction="E" />
      <neighbor name="Switzerland" direction="W" />
   </country>
   <country name="Singapore">
      <rank updated="yes">5</rank>
      <year>2011</year>
      <gdppc>59900</gdppc>
      <neighbor name="Malaysia" direction="N" />
   </country>
   <country name="Panama">
      <rank updated="yes">69</rank>
      <year>2011</year>
      <gdppc>13600</gdppc>
      <neighbor name="Costa Rica" direction="W" />
      <neighbor name="Colombia" direction="E" />
   </country>
</data>

import xml.etree.ElementTree as ET data = ""
"your xml here"
""
tree = ET.fromstring(data) print([el.attrib.get('name') for el in tree.findall('.//country')])
<?xml version="1.0"?><data>
   <country name="Liechtenstein">
      <rank updated="yes">2</rank>
      <year>2008</year>
      <gdppc>141100</gdppc>
      <neighbor name="Austria" direction="E" />
      <neighbor name="Switzerland" direction="W" />
   </country>
   <country name="Singapore">
      <rank updated="yes">5</rank>
      <year>2011</year>
      <gdppc>59900</gdppc>
      <neighbor name="Malaysia" direction="N" />
   </country>
   <country name="Panama">
      <rank updated="yes">69</rank>
      <year>2011</year>
      <gdppc>13600</gdppc>
      <neighbor name="Costa Rica" direction="W" />
      <neighbor name="Colombia" direction="E" />
   </country>
</data>
import xml.etree.ElementTree as ET tree = ET.parse(xmlfile) root = tree.getroot() names = root.findall("./country/@name")
import xml.etree.ElementTree as ET data = ""
"your xml here"
""
tree = ET.fromstring(data) print([el.attrib.get('name') for el in tree.findall('.//country')])
from lxml
import etree as ET data = ""
"your xml here"
""
tree = ET.fromstring(data) print(tree.xpath('//country/@name'))

Suggestion : 5

The element name, attribute names, and attribute values can be either bytestrings or Unicode strings. tag is the element name. attrib is an optional dictionary, containing element attributes. extra contains additional attributes, given as keyword arguments.,The element name, attribute names, and attribute values can be either bytestrings or Unicode strings. parent is the parent element. tag is the subelement name. attrib is an optional dictionary, containing element attributes. extra contains additional attributes, given as keyword arguments. Returns an element instance.,Opens a new element. tag is the element name. attrs is a dictionary containing element attributes. Returns the opened element.,Comment element factory. This factory function creates a special element that will be serialized as an XML comment by the standard serializer. The comment string can be either a bytestring or a Unicode string. text is a string containing the comment string. Returns an element instance representing a comment.

<?xml version="1.0"?>
<data>
   <country name="Liechtenstein">
      <rank>1</rank>
      <year>2008</year>
      <gdppc>141100</gdppc>
      <neighbor name="Austria" direction="E" />
      <neighbor name="Switzerland" direction="W" />
   </country>
   <country name="Singapore">
      <rank>4</rank>
      <year>2011</year>
      <gdppc>59900</gdppc>
      <neighbor name="Malaysia" direction="N" />
   </country>
   <country name="Panama">
      <rank>68</rank>
      <year>2011</year>
      <gdppc>13600</gdppc>
      <neighbor name="Costa Rica" direction="W" />
      <neighbor name="Colombia" direction="E" />
   </country>
</data>
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
root = ET.fromstring(country_data_as_string)
>>> root.tag 'data' >>>
   root.attrib {}
>>>
for child in root:
   ...print(child.tag, child.attrib)
   ...
   country {
      'name': 'Liechtenstein'
   }
country {
   'name': 'Singapore'
}
country {
   'name': 'Panama'
}
>>> root[0][1].text '2008'

Suggestion : 6

The syntax is similar to our xml module, so we’re still getting the attribute names using value = tag['attribute_name'] and text = tag.text. Exactly the same as before!,The tag.get(attribute) will get the value of our <attribute> tag at the levels which we are searching at. So, we simply need to do this at <header/type>, and get the values of the <heading> and the <text> attributes. That’s it!,To get the attribute value of name, we can do the same as before. We can also use tag.attrib[name] to get the value. This is the same as tag.get(name), except that it uses dictionary lookups.,Not only does it provide ease of access, since it is already installed, but it is also quite fast. Let’s look at exactly how we can extract attributes from our test file.

<page>
   <header>
      <type heading="XML Parsing in Python" />
      <type text="Hello from AskPython. We'll be parsing XML" />
   </header>
</page>
import xml.etree.ElementTree as ET
import xml.etree.ElementTree as ET
root_node = ET.parse('sample.xml').getroot()
print(root_node)
<Element 'page' at 0x7f885836b2f0>
for tag in root_node.find_all(level):
   value = tag.get(attribute)
if value is not None: print(value)
import xml.etree.ElementTree as ET

# We're at the root node (<page>)
   root_node = ET.parse('sample.xml').getroot()

   # We need to go one level below to get <header>
      # and then one more level from that to go to <type>
         for tag in root_node.findall('header/type'):
         # Get the value of the heading attribute
         h_value = tag.get('heading')
         if h_value is not None:
         print(h_value)
         # Get the value of the text attribute
         t_value = tag.get('text')
         if t_value is not None:
         print(t_value)

Suggestion : 7

ElementTree comes along with python.,example.py – Python Program,Python XML Parsing – We shall learn to parse xml documents in python programming language. There are many options available out there. We shall go through enough example for the following libraries

We shall consider following xml file for  examples going forward in this tutorial.

<?xml version="1.0" encoding="UTF-8" ?>

<holidays year="2017">
   <holiday type="other">
      <date>Jan 1</date>
      <name>New Year</name>
   </holiday>
   <holiday type="public">
      <date>Oct 2</date>
      <name>Gandhi Jayanti</name>
   </holiday>
</holidays>

example.py – Python Program

# Python XML Parsing
import xml.etree.ElementTree as ET
root = ET.parse('sample.xml').getroot()
tag = root.tag
print(tag)

Output

tutorialkart @arjun - VPCEH26EN: ~/PycharmProjects/PythonTutorial / parsing$ python python_xml_parse_ElementTree.py
holidays