python and xml integration [closed]
How do I pr开发者_JAVA技巧oduce XML from Python?
I built a program for keystroke detection integrated with open office and other softwares and i want the output of the program to be stored in an XML file?
my program detects the keys stroked in an open office, ms office software. i want the outpu to be directly stored in an XML file
You should use lxml Python library.
from lxml import etree
root = etree.Element("root")
root.set("foo", "bar")
child1 = etree.SubElement(root, "spam")
et = etree.ElementTree(root)
et.write('output_file.xml', xml_declaration=True, encoding='utf-8')
Look into xml.dom.minidom
, particulary the toXml
method.
If you're looking to be able to run a python function from within an xml file, take a look at this: lxml extension functions
With these, you can define a function namespace and then, with the proper editing of your xml/xsl file, you'll be able to put the value of your code into the xml file.
The other option would be to do as systempuntoout suggested and parse your xml into a python object using the lxml library, and then place the output of running your code in the proper place.
Either way, more specificity in your question will help us answer you quickly and correctly.
Edit:
If you're just looking to write the keystrokes to some file.xml
, you can use:
with open('file.xml', 'w') as f:
f.write(keystroke_data)
精彩评论