Next / Previous / Contents / TCC Help System / NM Tech homepage

12.6. An example of document creation

Here is a Python script that creates a small XHTML file and writes it in XML form to its standard output.

creator
#!/usr/local/bin/python
#================================================================
# creator:  Demonstrate building an XML file with a <!DOCTYPE>
#----------------------------------------------------------------

import Ft.Xml.Domlette as domlette

dom = domlette.implementation

doc = dom.createDocument(None, "html", None)
doc.publicId  =  "-//W3C//DTD XHTML 1.0 Strict//EN"
doc.systemId  =  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"

html=doc.documentElement
doc.appendChild(html)

head=doc.createElementNS(None, "head")
html.appendChild(head)

title=doc.createElementNS(None, "title")
head.appendChild(title)
title.appendChild(doc.createTextNode("Sample title"))

body=doc.createElementNS(None, "body")
html.appendChild(body)

p=doc.createElementNS(None, "p")
body.appendChild(p)
p.appendChild(doc.createTextNode("Sample paragraph text"))
p.setAttributeNS(None, "class", "sample")

domlette.PrettyPrint(doc)

Here is the output created by this script:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Sample title</title>
  </head>
  <body>
    <p class="sample">Sample paragraph text</p>
  </body>
</html>