[XML-SIG] Re: inline html using elemettree
Fredrik Lundh
fredrik at pythonware.com
Sun Aug 31 12:58:05 EDT 2003
Jason Thompson WROTE:
> I'm currently writing a blog-like system using elementtree to write the posts
> to xml. It's all working very nicely only the content element I created has
> to contain html markup. The problem is, when I write this to file using
> elementtree the html tags are escaped. Is there a way to write the posting to
> file using elementtree, but maintaining the html markup inside the content
> tags?
>
> e.g.
> <content><p>This is where the html content goes</p></content>
you cannot embed arbitrary HTML inside an XML document without
ending up with invalid XML.
if you cannot guarantee that the HTML is well-formed, you have to
escape it (this is how it's usually done in RSS, for example).
from elementtree.ElementTree import Element
content = Element("content")
content.text = html_as_text
if you can guarantee that the HTML is well-formed, parse it to XML,
and embed the resulting XML structure:
from elementtree.ElementTree import Element, XML
content = Element("content")
content.append(XML(html_as_text))
note that you can use the ElementTidy extension to convert arbitrary
HTML to well-formed (X)HTML; for details, see:
http://effbot.org/zone/element-tidylib.htm
</F>
More information about the XML-SIG
mailing list