File: generate_xml1.py

package info (click to toggle)
python-xml 0.8.4-10.1%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,972 kB
  • ctags: 10,628
  • sloc: python: 46,730; ansic: 14,354; xml: 968; makefile: 201; sh: 20
file content (39 lines) | stat: -rw-r--r-- 1,169 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
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.createDocument(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
    from xml.dom import ext
    ext.Print(doc)