XML/DOM serialization

< XML
Revision as of 19:49, 24 January 2007 by Adonis (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Create a simple DOM and having it serialize to:

Task
XML/DOM serialization
You are encouraged to solve this task according to the task description, using any language you may know.
 <?xml version="1.0" ?>
 <root>
     <element>
         Some text here
     </element>
 </root>

Python

Interpreter: Python 2.5

 from xml.dom.minidom import getDOMImplementation
 
 dom = getDOMImplementation()
 document = dom.createDocument(None, "root", None)
 
 topElement = document.documentElement
 firstElement = document.createElement("element")
 topElement.appendChild(firstElement)
 textNode = document.createTextNode("Some text here")
 firstElement.appendChild(textNode)
 
 xmlString = document.toprettyxml(" " * 4)