XML DOM: XML/XHTML inside a text node

Paul Boddie paul at boddie.org.uk
Thu Nov 3 08:16:14 EST 2005


Roman Suzi wrote:
>
> On Thu, 2 Nov 2005 noa... at gmail.com wrote:
> > Is there a better way?
>
> What about parsing the input into XML first? Is there any point in including
> unescaped code into XML document unless it is XML itself?

Indeed. My approach would be to parse the user's input using the
parseString function, to get the "root element" of this newly parsed
document, and then to import that element into the existing document
using importNode. The imported document information could then be
inserted into the existing document at a suitable place. For example:

user_doc = xml.dom.minidom.parseString(user_input)
# NOTE: Check for element or use XPath here...
user_doc_root = user_doc.childNodes[0]
imported_root = existing_doc.importNode(user_doc_root, 1)
existing_doc_location.appendChild(imported_root)

Paul




More information about the Python-list mailing list