Why is there no functional xml?
Rustom Mody
rustompmody at gmail.com
Sat Jan 20 11:06:12 EST 2018
Looking around for how to create (l)xml one sees typical tutorials like this:
https://www.blog.pythonlibrary.org/2013/04/30/python-101-intro-to-xml-parsing-with-elementtree/
Given the requirement to build up this xml:
<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>
</zAppointments>
the way I would rather do it is thus:
[Note in actual practice the 'contents' such as 1181251680 etc would come
from suitable program variables/function-calls
]
ex = Ea("zAppointments", {'reminder':'15'},
E("appointment",
En("begin", 1181251680),
Et("uid", "040000008200E000"),
En("alarmTime", 1181572063),
E("state"),
E("location"),
En("duration",1800),
Et("subject", "Bring pizza home")))
with the following obvious definitions:
[The function names are short so that the above becomes correspondingly readable]
from lxml.etree import Element
def Ea(tag, attrib=None, *subnodes):
"xml node constructor"
root = Element(tag, attrib)
for n in subnodes:
root.append(n)
return root
def E(tag, *subnodes):
"Like E but without attributes"
root = Element(tag)
for n in subnodes:
root.append(n)
return root
def Et(tag, text):
"A pure text node"
root = E(tag)
root.text = text
return root
def En(tag, text):
"A node containing a integer"
root = E(tag)
root.text = str(text)
return root
This approach seems so obvious that I find it hard to believe its not there somewhere…
Am I missing something??
More information about the Python-list
mailing list