0

I have tried this approach but it doesn't work for me.

I want to get the releaseDate value from the below xml

<Product prodID="bed" lang="en"> 
<ProductState stateType="Published" stateDateTime="2019-04" testDate="2019-04" releaseDate="2019"/>

I have tried the below code:

from pathlib import Path
import os
import tempfile
import xml.etree.ElementTree as ET

xmlfile = 'path_to_xml'
tree = ET.parse(xmlfile)
root = tree.getroot()

for elm in root.findall("./Product/ProductStatus/releaseDate"):
    print(elm.attrib)
newbie101
  • 65
  • 7

2 Answers2

1

Listing [Python.Docs]: xml.etree.ElementTree - The ElementTree XML API.
Considering this exact XML blob, there are 2 errors in your code:

  • The root node is Product node, so if you search for (other) Product sub-nodes it won't find anything

  • releaseDate is an attribute (not a tag) so it doesn't belong in the path

Here''s an example.

blob.xml

<?xml version="1.0" encoding="UTF-8"?>
<Product prodID="bed" lang="en"> 
    <ProductState stateType="Published" stateDateTime="2019-04" testDate="2019-04" releaseDate="2019"/>
    <!-- Other nodes -->
</Product>

code00.py:

#!/usr/bin/env python

import sys
import xml.etree.ElementTree as ET


def main(*argv):
    xmlfile = "./blob.xml"
    root = ET.parse(xmlfile).getroot()
    print("Root node:", root)
    for product_state_node in root.findall("ProductState"):
        print("Release date: ", product_state_node.attrib.get("releaseDate"))


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.\n")
    sys.exit(rc)

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q074401612]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" ./code00.py
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32

Root node: <Element 'Product' at 0x0000020698C4A860>
Release date:  2019

Done.

Although for this simple example it's might not be the case I prefer XPath when iterating XML trees. For more details you could check (there are many more):

CristiFati
  • 38,250
  • 9
  • 50
  • 87
0

With .get("attributeName") you can ask for the attribute value of your interest tag.

If you change your code to:

for elm in root.findall("ProductState"):
    print(elm.get("releaseDate"))

it will work, if Product is your root.

print(elm.attrib) # prints all attributes as a dic of key:values
Hermann12
  • 1,709
  • 2
  • 5
  • 14