{"id":4814,"date":"2025-01-22T13:07:15","date_gmt":"2025-01-22T13:07:15","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4814"},"modified":"2025-01-30T20:18:07","modified_gmt":"2025-01-30T20:18:07","slug":"write-xml","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/xml\/write-xml","title":{"rendered":"Write XML"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Create a new XML file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    #Create new XML\n    #root = ET.Element('MyRootNode')                  #Normal create\n    attributes = {\"ver\": \"1.0\"}                            #If you want attributes added to the root node\n    root = ET.Element('MyRootNode', attributes)\n\n    #----- CREATE ALL THE LEVEL 1 NODES -----\n    nodes_level1 = {}\n    nodes_level1&#91;'Brightness'] = ET.SubElement(root, 'Brightness')\n    nodes_level1&#91;'Colour'] = ET.SubElement(root, 'Colour')\n\n    #----- CREATE THE BRIGHTNESS NODE CONTENT -----\n    #Add data to one of the level 1 nodes\n    nodes_level2 = &#91;]\n    nodes_level2.append(ET.SubElement(nodes_level1&#91;'Brightness'], 'PointBlock'))\n    #Add attributes to that node\n    nodes_level2&#91;len(nodes_level2)-1].set(\"xleft\", \"0.0\")\n    nodes_level2&#91;len(nodes_level2)-1].set(\"xright\", \"32.0\")\n    \n    #Add sub nodes to it\n    nodes_level3 = &#91;]\n    \n    nodes_level3.append(ET.SubElement(nodes_level2&#91;len(nodes_level2)-1], 'Point'))\n    #Add attributes to that node\n    nodes_level3&#91;len(nodes_level3)-1].set(\"x\", \"0.0\")\n    nodes_level3&#91;len(nodes_level3)-1].set(\"y\", \"0.0\")\n    nodes_level3&#91;len(nodes_level3)-1].set(\"type\", \"1\")\n\n    nodes_level3.append(ET.SubElement(nodes_level2&#91;len(nodes_level2)-1], 'Point'))\n    #Add attributes to that node\n    nodes_level3&#91;len(nodes_level3)-1].set(\"x\", \"0.0\")\n    nodes_level3&#91;len(nodes_level3)-1].set(\"y\", \"0.0\")\n    nodes_level3&#91;len(nodes_level3)-1].set(\"type\", \"2\")\n\n    #All done, output the XML\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')<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Output<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version='1.0' encoding='utf8'?>\n&lt;MyRootNode ver=\"1.0\">\n &lt;Brightness>\n  &lt;PointBlock xleft=\"0.0\" xright=\"32.0\">\n   &lt;Point x=\"0.0\" y=\"0.0\" type=\"1\" \/>\n   &lt;Point x=\"0.0\" y=\"0.0\" type=\"2\" \/>\n  &lt;\/PointBlock>\n &lt;\/Brightness>\n &lt;Colour \/>\n&lt;\/MyRootNode><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output modified XML as a string<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  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')<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create a new XML file Output Output modified XML as a string<\/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-4814","post","type-post","status-publish","format-standard","hentry","category-xml"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4814","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=4814"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4814\/revisions"}],"predecessor-version":[{"id":4869,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4814\/revisions\/4869"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}