-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc27f0c
commit e42b2c3
Showing
6 changed files
with
77 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |