#!/usr/bin/env python import libxml2 import sys # Memory debug specific libxml2.debugMemory(1) doc = libxml2.newDoc("1.0") comment = doc.newDocComment("This is a generated document") doc.addChild(comment) pi = libxml2.newPI("test", "PI content") doc.addChild(pi) root = doc.newChild(None, "doc", None) ns = root.newNs("http://example.com/doc", "my") root.setNs(ns) elem = root.newChild(None, "foo", "bar") elem.setBase("http://example.com/imgs") elem.setProp("img", "image.gif") doc.saveFile("tmp.xml") doc.freeDoc() doc = libxml2.parseFile("tmp.xml") comment = doc.children if comment.type != "comment" or \ comment.content != "This is a generated document": print("error rereading comment") sys.exit(1) pi = comment.next if pi.type != "pi" or pi.name != "test" or pi.content != "PI content": print("error rereading PI") sys.exit(1)
#!/usr/bin/python -u import libxml2 import sys # Memory debug specific libxml2.debugMemory(1) doc = libxml2.newDoc("1.0") comment = doc.newDocComment("This is a generated document") doc.addChild(comment) pi = libxml2.newPI("test", "PI content") doc.addChild(pi) root = doc.newChild(None, "doc", None) ns = root.newNs("http://example.com/doc", "my") root.setNs(ns) elem = root.newChild(None, "foo", "bar") elem.setBase("http://example.com/imgs") elem.setProp("img", "image.gif") doc.saveFile("tmp.xml") doc.freeDoc() doc = libxml2.parseFile("tmp.xml") comment = doc.children if comment.type != "comment" or \ comment.content != "This is a generated document": print "error rereading comment" sys.exit(1) pi = comment.next if pi.type != "pi" or pi.name != "test" or pi.content != "PI content": print "error rereading PI" sys.exit(1)