-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeispiel_xml_write_file.py
More file actions
45 lines (39 loc) · 1.15 KB
/
Beispiel_xml_write_file.py
File metadata and controls
45 lines (39 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/local/bin/python3
##############################################
#
# Name: Beispiel_xml_write_file.py
#
# Author: Peter Christen
#
# Version: 1.0
#
# Date: 10.07.2017
#
# Purpose: Schreiben eines XML-Files
#
##############################################
import xml.dom.minidom
###Erste Stufe erstellen
doc = xml.dom.minidom.Document()
doc.appendChild(doc.createComment("Inventardaten von Server Andromeda"))
erste_stuffe = doc.createElement("inventar")
doc.appendChild(erste_stuffe)
###Zweite Stufe erstellen
zweite_stuffe = doc.createElement("server")
zweite_stuffe.setAttribute( "name", 'Andromeda' )
erste_stuffe.appendChild(zweite_stuffe)
###Datenelemente erstellen
data = {'nrcpu':'8','release':'16.6.0','system':'Darwin','machine':'x86_64','timestamp':'1499540757'}
for name,value in data.items():
# Element erstellen
element_name = doc.createElement(name)
zweite_stuffe.appendChild(element_name)
# Element Wert hinzufügen
element_wert = doc.createTextNode(value)
element_name.appendChild(element_wert)
###Daten in File schreiben
doc.writexml( open('inventar.xml', 'w'),
indent=" ",
addindent=" ",
newl='\n')
doc.unlink()