{"id":8364,"date":"2021-02-12T15:28:52","date_gmt":"2021-02-12T09:58:52","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8364"},"modified":"2021-02-12T15:28:53","modified_gmt":"2021-02-12T09:58:53","slug":"python-xml-parser-tutorial","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-xml-parser-tutorial\/","title":{"rendered":"Python XML Parser Tutorial"},"content":{"rendered":"\n<p>Extensible Markup language (XML) was developed in 1998 to make it both human and machine-readable, designed to store any amount of data, and is widely used for sharing information across the internet.\u00a0 In this, Python XML Parser Tutorial you will learn to parse and modify an XML document<\/p>\n\n\n\n<p>In XML there are no built-in tags like HTML has. We declare tags as we want.<\/p>\n\n\n\n<p>Python is a general-purpose programming language; it allows us to parse and modify an <a href=\"https:\/\/en.wikipedia.org\/wiki\/XML\" class=\"rank-math-link\" rel=\"nofollow noopener\" target=\"_blank\">XML document<\/a>. There are two main libraries in Python that deals with XML.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Minidom<\/li><li>ElementTree<\/li><\/ul>\n\n\n\n<p>Let\u2019s start with Minidom<\/p>\n\n\n\n<p>Let\u2019s first create a simple XML file that we are going to use in this tutorial. Create a file with the name notes.xml as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;note>\n\u00a0 &lt;to>Tove&lt;\/to>\n\u00a0 &lt;from>Jani&lt;\/from>\n\u00a0 &lt;heading>Reminder&lt;\/heading>\n\u00a0 &lt;body>Don't forget me this weekend!&lt;\/body>\n&lt;\/note><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/ZvHisoibtNnRVaRtS_ocBZRpdpuaPazGCJPlrxC3Y6lbALvadOY5222YRVOD4XvoZVWUQb1bi1ghGMFANdjLT1DPWVNwWp_tjWE87S-C4wiYvU0Or72mXahvwy9aInFV7vK6kcc\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Parse XML using minidom<\/h2>\n\n\n\n<p>Now let\u2019s parse the notes.xml file and print the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/how-to-set-up-node-npm\/\" class=\"rank-math-link\">node name<\/a> that is \u2018document\u2019 and first child tag name that is \u2018note\u2019 as we created in the XML file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import xml.dom.minidom\n\nnotes = xml.dom.minidom.parse('notes.xml')\nprint(notes.nodeName)\nprint(notes.firstChild.tagName)<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/I7ouORrmJRRiMT3qzD8DE7hP2SdDUYMq4ZzQS0E5OVAPxIZohsxmSCGy2DCAqqtNPofjhgbJJE8FC4Y-EP_YkpO6_OIeN8--VYWdAAgZ520A-bdnAlTEwtj9md-93tEYQuubYHM\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>Now let\u2019s print out the data in the first Child.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import xml.dom.minidom\n\nnotes = xml.dom.minidom.parse('notes.xml')\nnote = notes.getElementsByTagName(\"to\")\nfor i in note:\n\u00a0 print(i.firstChild.data)<\/pre>\n\n\n\n<p>The following will be the output.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/shb0EAtnhfKCVKDOiPsrJE0v1OcTf2WD3sN57VA1eXb-_tJKVaE-WzdMSJGA-tZwKHVG-BmUM1rI_qFWDSMdmoz8YK1fzNo5lAz9ue4QLjLYXxAMBxhBitaApGxoaYhYtxaKe84\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Parse XML using ElementTree<\/h2>\n\n\n\n<p>ElementTree is easy as compared to Minidom. Let\u2019s take a look at how to parse XML file using ElementTree.<\/p>\n\n\n\n<p>We just need the following code to print all the data in our notes.xml file.<\/p>\n\n\n\n<p>The following will be the output.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/5DXizOp23_R3C2lsjt9PMONyE16Lyph-cIH_xRue0kHiQwHuAq7hBpC7ZyXOBc-LhV5yCSkrKQElayM_alBBazse7dFoDW29UcxIKD-nAfxfRtbM025PIIUi9Em-nZtTRj7wJDA\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>Let\u2019s write data to XML file using ElementTree<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import xml.etree.ElementTree as ET\n\n# create the file structure\ndata = ET.Element('notes')\nitems = ET.SubElement(data, 'Persons')\nitem1 = ET.SubElement(items, 'person1')\nitem2 = ET.SubElement(items, 'person2')\nitem1.set('name','salman')\nitem2.set('name','Ahmed')\nitem1.text = 'Hello Salman'\nitem2.text = 'Hello Ahmed'\n\n# create a new XML file with the results\nmydata = ET.tostring(data)\nmyfile = open(\"notes1.xml\", \"wb\")\nmyfile.write(mydata)<\/pre>\n\n\n\n<p>The following will be the output.<img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/-jQmp6D4ZbsE5WzMQCD8R30tbajGZzVk_rrP1vNlEnbPc_-xMGIIirPxLkX-d1J3FzX-0xiEgsZO-hbrcAqA_O8MlXWZJ_6xzMMw8fe6H2XXFHr4hdB8-iSpbRZAI716vkLbxNM\" width=\"624\" height=\"664\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>The following will be the output of XML file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/ywNSE2AJOzDMHbvbYB7_vD6_zjKU2vTTTKO163Ww0YXSjOfIAsSiQEWCxhOTAZG0KL3rSLbvrn20UYwQwzhNCl7OyDrp-g4frLowVMW9pq_y1o3nNP2Ls6qZa77E_u2J_MUnisw\" alt=\"\" title=\"\"><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Extensible Markup language (XML) was developed in 1998 to make it both human and machine-readable, designed to store any amount of data, and is widely used for sharing information across the internet.\u00a0 In this, Python XML Parser Tutorial you will learn to parse and modify an XML document In XML there are no built-in tags [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8367,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-8364","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8364"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8364\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8367"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}