Download file as xml

Good morning Gerardo,

Yes, but you'll have to generate the XML piece by piece. I would use my helper script module for this kind of work: xml.py (Place it in your project script library.)

Make a somewhat generic row-to-xml function in a project library, perhaps like so:

rowTemplate = """<table><row/></table>"""

# Produce downloadable XML bytes from a dictionary of column values.
def rowToXML(valuesDict):
	dom = xml.strToDoc(rowTemplate)
	rowNode = dom.getElementsByTagName("row")[0]
	for k, v in valuesDict.items():
		cellNode = dom.createElement("cell")
		cellNode.setAttribute("columnName", k)
		cellNode.setTextContent(unicode(v))
		rowNode.appendChild(cellNode)
	return xml.docToBytes(dom)

Then, in your download button script, you'd have something like this:

	output = myLibraryScript.rowToXML(xml.OrderedDict(self.path.to.row.mapping))
	system.perspective.download("suggestedFilename.xml", output)

The path.to.row.mapping might just point at your table's selection data. It needs to be a single Perspective object mapping (key/value pairs). Though you could expand the example to handle multiple dictionaries.

Fundamentally, working with XML follows this model:

  • Start with some DOM, typically with a top level template,

  • Obtain a node to manipulate with a dom.getElement***()

  • Create a new node from the DOM, set its attributes, and append it to some other node.

  • Set content on nodes as needed.

  • Repeat as needed.

  • Transform the modified DOM to bytes or to a string for delivery.

3 Likes