How do you create an xml file using a Python script?

I am trying to create an xml file that contains formatted information from a Revit model for input into another program, and need to use a python script to create it. I can create an equivalent txt file by using the following commands:

file = open(“C:\Users\username\Documents\filename.txt”)

file.write(“info to write to file”)

file.close()

But even after changing the extension to .xml, the file still isn’t recognized as a real xml file. Any thoughts on how properly create an xml file with a python script?

 

I have not used them myself, but note there are some XML nodes in Lunchbox and Bakery

It might be worth trying these first, rather than Python

Andrew

Hi Tony,

For more information look here http://www.theprovingground.org/2015/04/integrating-xml-with-dynamo-via-lunchbox.html

And if you want to Read xml file you can use custom nodes from bakery package.

Thanks guys! The Bakery package didn’t exactly do what I needed, but it helped me figure out the missing pieces to make it work.

Here is quick example of a script to make an xml file and the resulting output file:

Glad Bakery helped a bit Tony! Will you publish any of your work or code as a custom node? The ET part is that ElementTree is being used as the xml ‘engine’. ElementTree comes with IronPython.

I’d like to integrate some of your XML writing code back into Bakery, if you get it to a nice logical point feel free to reply or email it to me.

1 Like