1 #!/usr/bin/env python 2 3 import libxml2dom, xml.dom.minidom 4 5 document = libxml2dom.createDocument(None, "doc", None) 6 top = document.xpath("*")[0] 7 elem1 = document.createElementNS("DAV:", "href") 8 document.replaceChild(elem1, top) 9 elem2 = document.createElementNS(None, "no_ns") 10 document.xpath("*")[0].appendChild(elem2) 11 print document.toString() 12 document.toFile(open("test_ns.xml", "wb")) 13 14 document = libxml2dom.parse("test_ns.xml") 15 print "Namespace is", repr(document.xpath("*")[0].namespaceURI) 16 17 document = xml.dom.minidom.Document() 18 elem1 = document.createElementNS("DAV:", "href") 19 document.appendChild(elem1) 20 elem2 = document.createElementNS(None, "no_ns") 21 document.childNodes[0].appendChild(elem2) 22 print document.toxml() 23 open("test_ns.xml", "wb").write(document.toxml()) 24 25 document = xml.dom.minidom.parse("test_ns.xml") 26 print "Namespace is", repr(document.documentElement.namespaceURI) 27 28 try: 29 from xml.dom.ext import PrettyPrint 30 PrettyPrint(document) 31 PrettyPrint(document, stream=open("test_ns.xml", "wb")) 32 33 document = xml.dom.minidom.parse("test_ns.xml") 34 print "Namespace is", repr(document.documentElement.namespaceURI) 35 36 except ImportError: 37 print "Prettyprinted document not produced." 38 39 # vim: tabstop=4 expandtab shiftwidth=4