tags:

views:

85

answers:

3

How do I produce 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

A: 

Look into xml.dom.minidom, particulary the toXml method.

Hank Gay
+3  A: 

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')
systempuntoout
A: 

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)
nearlymonolith
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.
sagar
What kind of XML structure are you looking for? If you're just seeking to write the keystrokes to a file with a `.xml` extension, you can use the typical `with open('file.xml', 'w') as f: f.write(keystrokedata)` construction. I'll edit my answer to include this.
nearlymonolith