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