{"id":4496,"date":"2023-11-30T11:31:58","date_gmt":"2023-11-30T11:31:58","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4496"},"modified":"2025-12-09T13:45:13","modified_gmt":"2025-12-09T13:45:13","slug":"read-xml","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/xml\/read-xml","title":{"rendered":"Read XML"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Read XML value from a XML string<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import xml.etree.ElementTree as ET\n\n    root = None\n    try:\n        tree = ET.ElementTree(ET.fromstring(my_string))          #Load the XML as an element tree object\n        root = tree.getroot()                   #Get root element \n    except ET.ParseError as Error:\n        print(\"Invalid XML received\")\n        print(Error)\n        root = None\n\n    if root != None:\n        #Look for an item in the XML root called &lt;Result>\n        for item in root.findall('Result'): \n            print(item.text)\n            if item.text == \"Success\":\n                print(\"Found success result\")\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Read XML value from an XML file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import xml.etree.ElementTree as ET\n\n    tree = ET.parse('my_xml_fiil.xml')          #Load the XML as an element tree object\n    root = tree.getroot()                   #Get root element \n  \n    #Look for an item in the XML root called &lt;Result&gt;\n    for item in root.findall('Result'): \n        print(item.text)\n        if item.text == \"Success\":\n            print(\"Found success result\")\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">In node present?<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    if (len(root.findall('Position')) == 0):<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Is item empty?<\/h4>\n\n\n\n<p>Item is empty if it has no text and no child elements<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (item.text != None) or len(list(item)):<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Parse XML values from an XML file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import xml.etree.ElementTree as ET\n\n    # create element tree object \n    tree = ET.parse('my_xml_file.xml')          #Load the XML as an element tree object\n    root = tree.getroot()                       #Get root element \n  \n    #Create empty list\n    my_items_list = &#91;] \n  \n    #Iterate through the XML items \n    for item in root.findall('.\/something\/item'): \n  \n        \n        my_dictionary = {}          #Create empty dictionary \n  \n        #Iterate through the child elements of this XML item \n        for child in item: \n            if child .text != None:\n                print(\"Found item: \" + child .tag + \"-\" + child .text)\n                my_dictionary&#91;child.tag] = str(child.text)    #&lt;&lt;Use str() if you want to stop Python turning numeric values into bytes etc\n  \n        my_items_list.append(my_dictionary) \n      \n    return my_items_list<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Read attribute value in a sub node<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    for item in root.findall('Position'):\n        for item2 in item.findall('MovementBlock'):\n            width = item2.attrib&#91;'width']    #Get existing value<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Read XML value from a XML string Read XML value from an XML file In node present? Is item empty? Item is empty if it has no text and no child elements Parse XML values from an XML file Read attribute value in a sub node<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[360],"tags":[],"class_list":["post-4496","post","type-post","status-publish","format-standard","hentry","category-xml"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4496"}],"version-history":[{"count":11,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4496\/revisions"}],"predecessor-version":[{"id":4971,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4496\/revisions\/4971"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}