xml version zAppointments reminder 15 appointment begin 1181251680 beg

  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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
<?xml version="1.0" ?>
<zAppointments reminder="15">
<appointment>
<begin>1181251680</begin>
<uid>040000008200E000</uid>
<alarmTime>1181572063</alarmTime>
<state></state>
<location></location>
<duration>1800</duration>
<subject>Bring pizza home</subject>
</appointment>
<appointment>
<begin>1234360800</begin>
<duration>1800</duration>
<subject>Check MS Office website for updates</subject>
<location></location>
<uid>604f4792-eb89-478b-a14f-dd34d3cc6c21-1234360800</uid>
<state>dismissed</state>
</appointment>
</zAppointments>
"""
from lxml import etree, objectify
#----------------------------------------------------------------------
def create_appt(data):
"""
Create an appointment XML element
"""
appt = objectify.Element("appointment")
appt.begin = data["begin"]
appt.uid = data["uid"]
appt.alarmTime = data["alarmTime"]
appt.state = data["state"]
appt.location = data["location"]
appt.duration = data["duration"]
appt.subject = data["subject"]
return appt
#----------------------------------------------------------------------
def create_xml():
"""
Create an XML file
"""
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<zAppointments>
</zAppointments>
'''
root = objectify.fromstring(xml)
root.set("reminder", "15")
appt = create_appt({"begin":1181251680,
"uid":"040000008200E000",
"alarmTime":1181572063,
"state":"",
"location":"",
"duration":1800,
"subject":"Bring pizza home"}
)
root.append(appt)
uid = "604f4792-eb89-478b-a14f-dd34d3cc6c21-1234360800"
appt = create_appt({"begin":1234360800,
"uid":uid,
"alarmTime":1181572063,
"state":"dismissed",
"location":"",
"duration":1800,
"subject":"Check MS Office website for updates"}
)
root.append(appt)
# remove lxml annotation
objectify.deannotate(root)
etree.cleanup_namespaces(root)
# create the xml string
obj_xml = etree.tostring(root,
pretty_print=True,
xml_declaration=True)
try:
with open("example.xml", "wb") as xml_writer:
xml_writer.write(obj_xml)
except IOError:
pass
#----------------------------------------------------------------------
if __name__ == "__main__":
create_xml()
"""
<zAppointments py:pytype="TREE" reminder="15">
<appointment py:pytype="TREE">
<begin py:pytype="int">1181251680</begin>
<uid py:pytype="str">040000008200E000</uid>
<alarmTime py:pytype="int">1181572063</alarmTime>
<state py:pytype="str"/>
<location py:pytype="str"/>
<duration py:pytype="int">1800</duration>
<subject py:pytype="str">Bring pizza home</subject>
</appointment><appointment py:pytype="TREE">
<begin py:pytype="int">1234360800</begin>
<uid py:pytype="str">604f4792-eb89-478b-a14f-dd34d3cc6c21-1234360800</uid>
<alarmTime py:pytype="int">1181572063</alarmTime>
<state py:pytype="str">dismissed</state>
<location py:pytype="str"/>
<duration py:pytype="int">1800</duration>
<subject py:pytype="str">Check MS Office website for updates</subject>
</appointment>
</zAppointments>
"""