{"id":4812,"date":"2025-01-22T13:06:26","date_gmt":"2025-01-22T13:06:26","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4812"},"modified":"2025-01-30T19:59:35","modified_gmt":"2025-01-30T19:59:35","slug":"modify-xml","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/xml\/modify-xml","title":{"rendered":"Modify XML"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Remove a node<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>root.remove(item)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Add an empty node<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>root.append(ET.Element('MyNodeName'))<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Replace a node with an empty node<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    root.remove(item)\n    root.append(ET.Element('Strobe'))<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Edit an 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\n\n            item2.set('width', '10')         #Set a new value\n            xml_string = ET.tostring(root, encoding='utf8').decode('utf8')    #Output the new XML<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Refresh the ElementTree object<\/h4>\n\n\n\n<p>You shouldn&#8217;t need to. If for some reason you do then you can do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    #Get the XML as a string again after the changes we've made\n    ET.indent(root, space=\" \", level=0)         #Make the XML nice to read for humans (add indents and line breaks)\n    data = ET.tostring(root, encoding='utf8').decode('utf8')\n\n    #Reload the xml\n    tree = ET.ElementTree(ET.fromstring(data))      #Load the XML as an element tree object\n    root = tree.getroot()                           #Get root element <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Remove a node Add an empty node Replace a node with an empty node Edit an attribute value in a sub node Refresh the ElementTree object You shouldn&#8217;t need to. If for some reason you do then you can do this:<\/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-4812","post","type-post","status-publish","format-standard","hentry","category-xml"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4812","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=4812"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4812\/revisions"}],"predecessor-version":[{"id":4864,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4812\/revisions\/4864"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}