[XML-SIG] PyXML and DOM example 'generate_xml1.py'
David Lucena
yakumoklesk at yahoo.es
Sun Sep 14 12:40:13 CEST 2008
I've started to use PyXML because I needed a xml library for a tool I am making. Due to lack of documentation, I have been looking for the examples, but I've found some of them does not work. In fact, the fist I tested was the one on the Subject. I have made some modifications get it to work. Here I paste the full file contents:
"""
A basic example of using the DOM to create an XML document from scratch.
"""
from xml.dom import ext
from xml.dom import implementation
if __name__ == '__main__':
#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. This automatically creates a document element
#which is the single element child of the document
doc = implementation.
( None, 'mydoc', dt)
#Get the document element
doc_elem = doc.documentElement
#Create an element: the Document instanmce acts as a factory
new_elem = doc.createElementNS( None, 'spam')
#Create an attribute on the new element
new_elem.setAttributeNS( None, 'eggs', 'sunnysideup')
#Create a text node
new_text = doc.createTextNode('some text here...')
#Add the new text node to the new element
new_elem.appendChild(new_text)
#Add the new element to the document element
doc_elem.appendChild(new_elem)
#Print out the resulting document
import xml.dom.ext
xml.dom.ext.Print(doc)
Basic changes have been:
-Change createHTMLDocument for createDocument
-Change '' for None in, createDocument, createElementNS, createAttributeNS
-Change the two lines from "xml.doc.*" to "xml.dom.*"
Please, report if I am wrong or not.
Thanks,
David.
PS.- I have changed createHTMLDocument for createDocument because first only takes 2 arguments whereas second takes four, as it reported the function from inspect module 'getargspec'
More information about the XML-SIG
mailing list