-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxml_example2.py
More file actions
66 lines (53 loc) · 1.81 KB
/
xml_example2.py
File metadata and controls
66 lines (53 loc) · 1.81 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
"""
Example of how to save data as xml, using the element tree module
This version does the full-on nested XML
"""
import xml.etree.ElementTree as ET
from indent_etree import indent # for prettier output
outfilename = "add_book_data2.xml"
# get the data from the py file
from add_book_data import AddressBook
# build a tree structure
root = ET.Element("address_book")
# add the elements:
for person in AddressBook:
p = ET.SubElement(root, "person")
# This method stores everything as sub-elements
for key, value in person.items():
if type(value) == dict:
address = ET.SubElement(p, 'address')
for sub_key, sub_value in value.items():
sub_el = ET.SubElement(address, sub_key)
sub_el.text=sub_value
else:
el = ET.SubElement(p, key)
el.text=value
# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)
indent(tree.getroot()) # to make it more pretty
tree.write(outfilename)
### See if we can re-load it
tree = ET.parse(outfilename)
book = tree.getroot()
# re-build the original list:
AddressBook2 = []
for person in list(book):
p = {}
for sub_el in list(person):
if sub_el.tag == "address":
address = {}
for sub_sub_el in sub_el.getchildren():
t = sub_sub_el.text
if t is None: ## etree returns None for empty tags!
address[sub_sub_el.tag] = ""
else:
address[sub_sub_el.tag] = t
p['address'] = address
else:
p[sub_el.tag] = sub_el.text
AddressBook2.append(p)
if AddressBook2 == AddressBook:
print "xml version is the same as the original"
else:
print "xml version is not exactly the same as the original"