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 print "Namespace is", repr(elem1.namespaceURI) 9 document.replaceChild(elem1, top) 10 elem2 = document.createElementNS(None, "no_ns") 11 print "Namespace is", repr(elem2.namespaceURI) 12 document.xpath("*")[0].appendChild(elem2) 13 print document.toString() 14 document.toFile(open("test_ns.xml", "wb")) 15 16 document = libxml2dom.parse("test_ns.xml") 17 print "Namespace is", repr(document.xpath("*")[0].namespaceURI) 18 19 document = xml.dom.minidom.Document() 20 elem1 = document.createElementNS("DAV:", "href") 21 print "Namespace is", repr(elem1.namespaceURI) 22 document.appendChild(elem1) 23 elem2 = document.createElementNS(None, "no_ns") 24 print "Namespace is", repr(elem2.namespaceURI) 25 document.childNodes[0].appendChild(elem2) 26 print document.toxml() 27 open("test_ns.xml", "wb").write(document.toxml()) 28 29 document = xml.dom.minidom.parse("test_ns.xml") 30 print "Namespace is", repr(document.documentElement.namespaceURI) 31 32 try: 33 from xml.dom.ext import PrettyPrint 34 PrettyPrint(document) 35 PrettyPrint(document, stream=open("test_ns.xml", "wb")) 36 37 document = xml.dom.minidom.parse("test_ns.xml") 38 print "Namespace is", repr(document.documentElement.namespaceURI) 39 40 except ImportError: 41 print "Prettyprinted document not produced." 42 43 # vim: tabstop=4 expandtab shiftwidth=4