Using ElementTree Python library, I need to find the element display-name and parent:
This is the Python code that is including also the sample XML:
import xml.etree.ElementTree as ET
ns = {'d': 'http://www.demandware.com/xml/impex/catalog/2006-10-31'}
root = ET.fromstring("""<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://www.demandware.com/xml/impex/catalog/2006-10-31" catalog-id="my-catalog-id">
<category category-id="my_id1">
<display-name xml:lang="cs-CZ">Bla bla 1</display-name>
<online-flag>false</online-flag>
</category>
<category category-id="my_id2">
<display-name xml:lang="cs-CZ">Bla bla 2</display-name>
<online-flag>false</online-flag>
<parent>my_id1</parent>
</category>
</catalog>
""")
def parse_category(category_node):
category_id = category_node.attrib['category-id']
is_online = category_node.find('d:online-flag', ns).text == "true"
display_name_node = category_node.find('d:display-name', ns) # FIXME
display_name = display_name_node.text if display_name_node else ''
parent_node = category_node.find('d:parent', ns)
parent_id = parent_node.text if parent_node else ''
print("{} | {} | {} | {}".format(category_id, display_name, is_online, parent_id))
for child in root:
print(child)
if child.tag == "{{{0}}}category".format(ns['d']):
parse_category(child)
Altough the display_name_node and parent_node variable are available for the node with id "my_id2", the text is not extracted in display_name and parent_id variables