1 #!/usr/bin/env python 2 3 import macrotest 4 import xml.dom.minidom 5 import libxml2dom 6 import xml.dom 7 import time 8 import sys 9 10 def find_root(d): 11 root = None 12 13 for n in d.childNodes: 14 if n.nodeType == xml.dom.Node.ELEMENT_NODE: 15 root = n 16 break 17 18 return root 19 20 def test_import(d): 21 d2 = xml.dom.minidom.getDOMImplementation().createDocument("nsD", "newdoc", None) 22 imported = d2.importNode(find_root(d), 1) 23 d2.replaceChild(imported, find_root(d2)) 24 return d, d2 25 26 def test_import2(d): 27 d2 = libxml2dom.createDocument("nsD", "newdoc", None) 28 imported = d2.importNode(find_root(d), 1) 29 d2.replaceChild(imported, find_root(d2)) 30 return d, d2 31 32 if __name__ == "__main__": 33 if sys.argv[2] == "macrotest": 34 35 x2_d = macrotest.parseFile(sys.argv[1]) 36 37 t = time.time() 38 x2_d1, x2_d2 = macrotest.test_import(x2_d) 39 macrotest.toFile(x2_d2, "/tmp/xxx_macrotest.xml") 40 print "Time", time.time() - t, "seconds" 41 42 elif sys.argv[2] == "minidom": 43 44 d = xml.dom.minidom.parse(sys.argv[1]) 45 46 t = time.time() 47 d1, d2 = test_import(d) 48 open("/tmp/xxx_minidom.xml", "wb").write(d2.toxml("utf-8")) 49 print "Time", time.time() - t, "seconds" 50 51 elif sys.argv[2] == "libxml2dom": 52 53 d = libxml2dom.parse(sys.argv[1]) 54 55 t = time.time() 56 d1, d2 = test_import2(d) 57 libxml2dom.toStream(d2, open("/tmp/xxx_libxml2dom.xml", "wb")) 58 print "Time", time.time() - t, "seconds" 59 60 # vim: tabstop=4 expandtab shiftwidth=4