Learning python by example

Python XML & XSLT

Create and add elements

from xml.dom.minidom import parseString

doc = parseString(u'<top/>'.encode('UTF-8'))

<?xml version="1.0" ?>
<top/>

top_element=doc.documentElement

element1=doc.createElementNS(None,u'section1')

top_element.appendChild(element1)

element1.appendChild(doc.createElementNS(None,u'subsection1'))

text1=doc.createTextNode(u'My first text')

element1.firstChild.appendChild(text1)

element1.appendChild(doc.createElementNS(None,u'subsection2'))

text2=doc.createTextNode(u'My second text')
element1.lastChild.appendChild(text2)

element1.firstChild.appendChild(text2)
element1.lastChild.appendChild(text1)

LearnPython (last edited 2008-11-15 13:59:36 by localhost)

Unable to edit the page? See the FrontPage for instructions.