-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxml_example.py
More file actions
46 lines (32 loc) · 1.06 KB
/
xml_example.py
File metadata and controls
46 lines (32 loc) · 1.06 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/bin/env python
"""
Example of how to save data as xml, using the element tree module
This version stores all the data as attributes
"""
import xml.etree.ElementTree as ET
from indent_etree import indent # for prettier output
outfilename = "add_book_data.xml"
# get the data from the py file
from add_book_data_flat 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 in attributes
for key,value in person.items():
p.set(key, 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 book.getchildren():
#print person.attrib
AddressBook2.append(person.attrib)
if AddressBook2 == AddressBook:
print "xml version is the same as the original"