Skip to content

Commit

Permalink
Creating and Manipulating XML
Browse files Browse the repository at this point in the history
  • Loading branch information
artuguen28 committed Nov 3, 2021
1 parent cc27f0c commit e42b2c3
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 36 deletions.
5 changes: 2 additions & 3 deletions XML_Processing/DOM_module.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import xml.dom.minidom
import os


domtree = xml.dom.minidom.parse("XML_Processing\group.xml")
group = domtree.documentElement
Expand All @@ -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]

30 changes: 30 additions & 0 deletions XML_Processing/creating_elements.py
Original file line number Diff line number Diff line change
@@ -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"))

33 changes: 0 additions & 33 deletions XML_Processing/group.xml
Original file line number Diff line number Diff line change
@@ -1,33 +0,0 @@
<?xml version='1.0'?> <!--Indicates the version of XML-->
<group>
<person id="1">
<name>Jonh Smith</name>
<age>20</age>
<weight>80</weight>
<height>188</height>
</person>
<person id="2">
<name>Mike Davis</name>
<age>45</age>
<weight>82</weight>
<height>185</height>
</person>
<person id="3">
<name>Anna Johnson</name>
<age>33</age>
<weight>67</weight>
<height>167</height>
</person>
<person id="4">
<name>Bob Smith</name>
<age>60</age>
<weight>70</weight>
<height>174</height>
</person>
<person id="5">
<name>Sarah Pitt</name>
<age>12</age>
<weight>50</weight>
<height>152</height>
</person>
</group>
14 changes: 14 additions & 0 deletions XML_Processing/manipulating_files.py
Original file line number Diff line number Diff line change
@@ -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"))
4 changes: 4 additions & 0 deletions XML_test/gfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" ?>
<root>
<product name="Geeks for Geeks"/>
</root>
27 changes: 27 additions & 0 deletions XML_test/xml_file_test.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e42b2c3

Please sign in to comment.