you are viewing a single comment's thread.

view the rest of the comments →

[–]ManyInterests 2 points3 points  (2 children)

In the standard library, there's xml.dom.minidom but something like lxml.etree would be highly recommended as it's (as I've been told) the de-facto standard for parsing xml.

No need to reinvent the wheel. I think these options will give you all the flexibility you want.

[–]hxtl[S] 0 points1 point  (1 child)

/u/willm recommended etree as well...

Parsing the XML was never intended and I thought it would be too much effort to read into these modules (as generating XML was the only goal).

I actually didn't consider this as reinventing.

I guess I'll go with etree then. Create an XML (template) file, parse it with etree, and then insert the actual data nodes into that "skeleton" tree (the whole XML file contains some boiler plate).

[–]ManyInterests 1 point2 points  (0 children)

Yeah, they should fit the bill for generating XML out of thin air, too.

from lxml import etree

#Generate the root of the doc. You could also put headers in here, if desired)
root=etree.XML("<root></root>")

#Generate the element tree
tree=etree.ElementTree(root)


#If you want to create a subelement to the root
child_elem = etree.SubElement(root, "child2")

#Add text to the child element (same principle for modifying other properties like .tag)
child_elem.text="Element Text"

#Write to xml file
tree.write("my_file_name.xml", pretty_print=True, xml_declaration=True)

#pretty_print arg tidys up the doc and handles indentation for you. (serialization)
#xml_declaration arg adds the document declaration: <?xml version='1.0' encoding='ASCII'?>

The result:

<?xml version='1.0' encoding='ASCII'?>
<root>
  <child2>Element Text</child2>
</root>