xml.dom

Uche Ogbuji uche at ogbuji.net
Fri Dec 8 16:40:55 CET 2000


Magnus Heino wrote:

> How do I build an xmldocument using something in xml.dom?

Here's an example:

from xml.dom import implementation

#Create a doctype using document type name, sysid and pubid
dt = implementation.createDocumentType('mydoc', '', '')

#Create a document using document element namespace URI, doc element
name and doctype
doc = implementation.createHTMLDocument('', 'mydoc', dt)

doc_elem = doc.documentElement

new_elem = doc.createElementNS('', 'spam')

new_elem.setAttributeNS('', 'eggs', 'sunnysideup')

new_text = doc.createTextNode('some text here...')

new_elem.appendChild(new_text)

doc_elem.appendChild(new_elem)

import xml.doc.ext

xml.doc.ext.Print(doc)



Should yield

<?xml version="1.0" encoding="UTF-8"?>
<mydoc><spam eggs="sunnysideup"><some text here.../spam></mydoc>


Though I'm typing off-head.

Does that help, or do you need any other info?

Big improvements in the docs are already in the works.

Thanks.


-- 
Uche Ogbuji
Personal:   uche at ogbuji.net		http://uche.ogbuji.net
Work:       uche.ogbuji at fourthought.com	http://Fourthought.com



More information about the Python-list mailing list