Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML writing tools for Python

Tags:

python

xml

xhtml

I'm currently trying ElementTree and it looks fine, it escapes HTML entities and so on and so forth. Am I missing something truly wonderful I haven't heard of?

This is similar to what I'm actually doing:

import xml.etree.ElementTree as ET root = ET.Element('html') head = ET.SubElement(root,'head') script = ET.SubElement(head,'script') script.set('type','text/javascript') script.text = "var a = 'I love &aacute; letters'" body = ET.SubElement(root,'body') h1 = ET.SubElement(body,'h1') h1.text = "And I like the fact that 3 > 1" tree = ET.ElementTree(root) tree.write('foo.xhtml')  # more foo.xhtml <html><head><script type="text/javascript">var a = 'I love &amp;aacute; letters'</script></head><body><h1>And I like the fact that 3 &gt; 1</h1> </body></html> 
like image 863
Vinko Vrsalovic Avatar asked Sep 11 '08 10:09

Vinko Vrsalovic


People also ask

Can you use XML with Python?

The Python standard library provides a minimal but useful set of interfaces to work with XML. The two most basic and broadly used APIs to XML data are the SAX and DOM interfaces. Simple API for XML (SAX) − Here, you register callbacks for events of interest and then let the parser proceed through the document.

How do you create an XML file in Python?

Creating XML Document using Python First, we import minidom for using xml. dom . Then we create the root element and append it to the XML. After that creating a child product of parent namely Geeks for Geeks.

Which XML parser is best in Python?

If parsing speed is a key factor for you, consider using cElementTree or lxml.

How do I load XML in Python?

Example Read XML File in Python To read an XML file, firstly, we import the ElementTree class found inside the XML library. Then, we will pass the filename of the XML file to the ElementTree. parse() method, to start parsing. Then, we will get the parent tag of the XML file using getroot() .


2 Answers

Another way is using the E Factory builder from lxml (available in Elementtree too)

>>> from lxml import etree  >>> from lxml.builder import E  >>> def CLASS(*args): # class is a reserved word in Python ...     return {"class":' '.join(args)}  >>> html = page = ( ...   E.html(       # create an Element called "html" ...     E.head( ...       E.title("This is a sample document") ...     ), ...     E.body( ...       E.h1("Hello!", CLASS("title")), ...       E.p("This is a paragraph with ", E.b("bold"), " text in it!"), ...       E.p("This is another paragraph, with a", "\n      ", ...         E.a("link", href="http://www.python.org"), "."), ...       E.p("Here are some reserved characters: <spam&egg>."), ...       etree.XML("<p>And finally an embedded XHTML fragment.</p>"), ...     ) ...   ) ... )  >>> print(etree.tostring(page, pretty_print=True)) <html>   <head>     <title>This is a sample document</title>   </head>   <body>     <h1 class="title">Hello!</h1>     <p>This is a paragraph with <b>bold</b> text in it!</p>     <p>This is another paragraph, with a       <a href="http://www.python.org">link</a>.</p>     <p>Here are some reservered characters: &lt;spam&amp;egg&gt;.</p>     <p>And finally an embedded XHTML fragment.</p>   </body> </html> 
like image 79
Peter Hoffmann Avatar answered Sep 21 '22 11:09

Peter Hoffmann


There's always SimpleXMLWriter, part of the ElementTree toolkit. The interface is dead simple.

Here's an example:

from elementtree.SimpleXMLWriter import XMLWriter import sys  w = XMLWriter(sys.stdout) html = w.start("html")  w.start("head") w.element("title", "my document") w.element("meta", name="generator", value="my application 1.0") w.end()  w.start("body") w.element("h1", "this is a heading") w.element("p", "this is a paragraph")  w.start("p") w.data("this is ") w.element("b", "bold") w.data(" and ") w.element("i", "italic") w.data(".") w.end("p")  w.close(html) 
like image 41
oasisbob Avatar answered Sep 23 '22 11:09

oasisbob