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