From e42b2c35ade56a79f229d05f4f6736dfc6e891d8 Mon Sep 17 00:00:00 2001 From: artuguen28 Date: Wed, 3 Nov 2021 12:32:56 -0400 Subject: [PATCH] Creating and Manipulating XML --- XML_Processing/DOM_module.py | 5 ++--- XML_Processing/creating_elements.py | 30 +++++++++++++++++++++++++ XML_Processing/group.xml | 33 ---------------------------- XML_Processing/manipulating_files.py | 14 ++++++++++++ XML_test/gfg.xml | 4 ++++ XML_test/xml_file_test.py | 27 +++++++++++++++++++++++ 6 files changed, 77 insertions(+), 36 deletions(-) create mode 100644 XML_Processing/creating_elements.py create mode 100644 XML_Processing/manipulating_files.py create mode 100644 XML_test/gfg.xml create mode 100644 XML_test/xml_file_test.py diff --git a/XML_Processing/DOM_module.py b/XML_Processing/DOM_module.py index 2f78535..2327c03 100644 --- a/XML_Processing/DOM_module.py +++ b/XML_Processing/DOM_module.py @@ -1,6 +1,4 @@ import xml.dom.minidom -import os - domtree = xml.dom.minidom.parse("XML_Processing\group.xml") group = domtree.documentElement @@ -12,7 +10,8 @@ if person.hasAttribute("id"): print("ID: %s" % person.getAttribute("id")) - name = person.getElementsByTagName ("name")[0] + name = person.getElementsByTagName("name")[0] age = person.getElementsByTagName("age")[0] weight = person.getElementsByTagName("weight")[0] height = person.getElementsByTagName("height")[0] + diff --git a/XML_Processing/creating_elements.py b/XML_Processing/creating_elements.py new file mode 100644 index 0000000..404b1a3 --- /dev/null +++ b/XML_Processing/creating_elements.py @@ -0,0 +1,30 @@ +import xml.dom.minidom + +domtree = xml.dom.minidom.parse("XML_Processing\group.xml") +group = domtree.documentElement + +# Creating new elements +newperson = domtree.createElement("person") +newperson.setAttribute("id", "6") + +name = domtree.createElement("name") +name.appendChild(domtree.createTextNode("Paul Smith")) + +age = domtree.createElement("age") +age.appendChild(domtree.createTextNode("45")) + +weight = domtree.createElement("weight") +weight.appendChild(domtree.createTextNode("78")) + +height = domtree.createElement("height") +height.appendChild(domtree.createTextNode("178")) + +newperson.appendChild(name) +newperson.appendChild(age) +newperson.appendChild(weight) +newperson.appendChild(height) + +group.appendChild(newperson) + +domtree.writexml(open("XML_Processing\group.xml", "w")) + diff --git a/XML_Processing/group.xml b/XML_Processing/group.xml index 947ee4d..e69de29 100644 --- a/XML_Processing/group.xml +++ b/XML_Processing/group.xml @@ -1,33 +0,0 @@ - - - - Jonh Smith - 20 - 80 - 188 - - - Mike Davis - 45 - 82 - 185 - - - Anna Johnson - 33 - 67 - 167 - - - Bob Smith - 60 - 70 - 174 - - - Sarah Pitt - 12 - 50 - 152 - - \ No newline at end of file diff --git a/XML_Processing/manipulating_files.py b/XML_Processing/manipulating_files.py new file mode 100644 index 0000000..0a27859 --- /dev/null +++ b/XML_Processing/manipulating_files.py @@ -0,0 +1,14 @@ +import xml.dom.minidom + +domtree = xml.dom.minidom.parse("XML_Processing\group.xml") +group = domtree.documentElement + +# Manipulating files +persons = group.getElementsByTagName("person") +# We adress the index zero persons[0] for the first element. +persons[0].getElementsByTagName("name")[0].childNodes[0].nodeValue = "New Name" + +# We can also change the attributes by using the function setAttribute. +persons[0].setAttribute("id", "1") + +domtree.writexml(open("XML_Processing\group.xml", "w")) diff --git a/XML_test/gfg.xml b/XML_test/gfg.xml new file mode 100644 index 0000000..e70e532 --- /dev/null +++ b/XML_test/gfg.xml @@ -0,0 +1,4 @@ + + + + diff --git a/XML_test/xml_file_test.py b/XML_test/xml_file_test.py new file mode 100644 index 0000000..eaec6eb --- /dev/null +++ b/XML_test/xml_file_test.py @@ -0,0 +1,27 @@ +from xml.dom import minidom +import os + +# We firt create a minidom document. +root = minidom.Document() + +# Names the main element 'root' of the dom. +xml = root.createElement('root') +# We insert it on the file. +root.appendChild(xml) + +# Creates the secondary element 'product'. +productChild = root.createElement('product') +# We add an attribute to it called 'name'. +productChild.setAttribute('name', 'Geeks for Geeks') +# We insert it on the secondary element. +xml.appendChild(productChild) + + +xml_str = root.toprettyxml(indent="\t") + +# Name of the xml file. +save_path_file = "gfg.xml" + +# Creates the actual file on the current path. +with open("XML_test\gfg.xml", "w") as f: + f.write(xml_str)