我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用xml.dom.documentElement()。
def testInsertBeforeFragment(self): dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes() dom.documentElement.insertBefore(frag, None) self.confirm(tuple(dom.documentElement.childNodes) == (orig, c1, c2, c3), "insertBefore(<fragment>, None)") frag.unlink() dom.unlink() dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes() dom.documentElement.insertBefore(frag, orig) self.confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3, orig), "insertBefore(<fragment>, orig)") frag.unlink() dom.unlink()
def testCloneDocumentDeep(self): doc = parseString("<?xml version='1.0'?>\n" "<!-- comment -->" "<!DOCTYPE doc [\n" "<!NOTATION notation SYSTEM 'http://xml.python.org/'>\n" "]>\n" "<doc attr='value'/>") doc2 = doc.cloneNode(1) self.confirm(not (doc.isSameNode(doc2) or doc2.isSameNode(doc)), "testCloneDocumentDeep: document objects not distinct") self.confirm(len(doc.childNodes) == len(doc2.childNodes), "testCloneDocumentDeep: wrong number of Document children") self.confirm(doc2.documentElement.nodeType == Node.ELEMENT_NODE, "testCloneDocumentDeep: documentElement not an ELEMENT_NODE") self.confirm(doc2.documentElement.ownerDocument.isSameNode(doc2), "testCloneDocumentDeep: documentElement owner is not new document") self.confirm(not doc.documentElement.isSameNode(doc2.documentElement), "testCloneDocumentDeep: documentElement should not be shared") if doc.doctype is not None: # check the doctype iff the original DOM maintained it self.confirm(doc2.doctype.nodeType == Node.DOCUMENT_TYPE_NODE, "testCloneDocumentDeep: doctype not a DOCUMENT_TYPE_NODE") self.confirm(doc2.doctype.ownerDocument.isSameNode(doc2)) self.confirm(not doc.doctype.isSameNode(doc2.doctype))
def testNormalizeCombineAndNextSibling(self): doc = parseString("<doc/>") root = doc.documentElement root.appendChild(doc.createTextNode("first")) root.appendChild(doc.createTextNode("second")) root.appendChild(doc.createElement("i")) self.confirm(len(root.childNodes) == 3 and root.childNodes.length == 3, "testNormalizeCombineAndNextSibling -- preparation") doc.normalize() self.confirm(len(root.childNodes) == 2 and root.childNodes.length == 2 and root.firstChild.data == "firstsecond" and root.firstChild is not root.lastChild and root.firstChild.nextSibling is root.lastChild and root.firstChild.previousSibling is None and root.lastChild.previousSibling is root.firstChild and root.lastChild.nextSibling is None , "testNormalizeCombinedAndNextSibling -- result") doc.unlink()
def testNormalizeDeleteWithPrevSibling(self): doc = parseString("<doc/>") root = doc.documentElement root.appendChild(doc.createTextNode("first")) root.appendChild(doc.createTextNode("")) self.confirm(len(root.childNodes) == 2 and root.childNodes.length == 2, "testNormalizeDeleteWithPrevSibling -- preparation") doc.normalize() self.confirm(len(root.childNodes) == 1 and root.childNodes.length == 1 and root.firstChild.data == "first" and root.firstChild is root.lastChild and root.firstChild.nextSibling is None and root.firstChild.previousSibling is None , "testNormalizeDeleteWithPrevSibling -- result") doc.unlink()
def testNormalizeDeleteWithNextSibling(self): doc = parseString("<doc/>") root = doc.documentElement root.appendChild(doc.createTextNode("")) root.appendChild(doc.createTextNode("second")) self.confirm(len(root.childNodes) == 2 and root.childNodes.length == 2, "testNormalizeDeleteWithNextSibling -- preparation") doc.normalize() self.confirm(len(root.childNodes) == 1 and root.childNodes.length == 1 and root.firstChild.data == "second" and root.firstChild is root.lastChild and root.firstChild.nextSibling is None and root.firstChild.previousSibling is None , "testNormalizeDeleteWithNextSibling -- result") doc.unlink()
def testNormalizeDeleteWithTwoNonTextSiblings(self): doc = parseString("<doc/>") root = doc.documentElement root.appendChild(doc.createElement("i")) root.appendChild(doc.createTextNode("")) root.appendChild(doc.createElement("i")) self.confirm(len(root.childNodes) == 3 and root.childNodes.length == 3, "testNormalizeDeleteWithTwoSiblings -- preparation") doc.normalize() self.confirm(len(root.childNodes) == 2 and root.childNodes.length == 2 and root.firstChild is not root.lastChild and root.firstChild.nextSibling is root.lastChild and root.firstChild.previousSibling is None and root.lastChild.previousSibling is root.firstChild and root.lastChild.nextSibling is None , "testNormalizeDeleteWithTwoSiblings -- result") doc.unlink()
def testNormalizeDeleteAndCombine(self): doc = parseString("<doc/>") root = doc.documentElement root.appendChild(doc.createTextNode("")) root.appendChild(doc.createTextNode("second")) root.appendChild(doc.createTextNode("")) root.appendChild(doc.createTextNode("fourth")) root.appendChild(doc.createTextNode("")) self.confirm(len(root.childNodes) == 5 and root.childNodes.length == 5, "testNormalizeDeleteAndCombine -- preparation") doc.normalize() self.confirm(len(root.childNodes) == 1 and root.childNodes.length == 1 and root.firstChild is root.lastChild and root.firstChild.data == "secondfourth" and root.firstChild.previousSibling is None and root.firstChild.nextSibling is None , "testNormalizeDeleteAndCombine -- result") doc.unlink()
def testGetElementsByTagName(self): dom = parse(tstfile) self.confirm(dom.getElementsByTagName("LI") == \ dom.documentElement.getElementsByTagName("LI")) dom.unlink()
def _create_fragment_test_nodes(self): dom = parseString("<doc/>") orig = dom.createTextNode("original") c1 = dom.createTextNode("foo") c2 = dom.createTextNode("bar") c3 = dom.createTextNode("bat") dom.documentElement.appendChild(orig) frag = dom.createDocumentFragment() frag.appendChild(c1) frag.appendChild(c2) frag.appendChild(c3) return dom, orig, c1, c2, c3, frag
def testAppendChild(self): dom = parse(tstfile) dom.documentElement.appendChild(dom.createComment("Hello")) self.confirm(dom.documentElement.childNodes[-1].nodeName == "#comment") self.confirm(dom.documentElement.childNodes[-1].data == "Hello") dom.unlink()
def testReplaceChildFragment(self): dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes() dom.documentElement.replaceChild(frag, orig) orig.unlink() self.confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3), "replaceChild(<fragment>)") frag.unlink() dom.unlink()
def testElement(self): dom = Document() dom.appendChild(dom.createElement("abc")) self.confirm(dom.documentElement) dom.unlink()
def testAAA(self): dom = parseString("<abc/>") el = dom.documentElement el.setAttribute("spam", "jam2") self.confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA") a = el.getAttributeNode("spam") self.confirm(a.ownerDocument is dom, "setAttribute() sets ownerDocument") self.confirm(a.ownerElement is dom.documentElement, "setAttribute() sets ownerElement") dom.unlink()
def testAAB(self): dom = parseString("<abc/>") el = dom.documentElement el.setAttribute("spam", "jam") el.setAttribute("spam", "jam2") self.confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB") dom.unlink()
def testChangeAttr(self): dom = parseString("<abc/>") el = dom.documentElement el.setAttribute("spam", "jam") self.confirm(len(el.attributes) == 1) el.setAttribute("spam", "bam") # Set this attribute to be an ID and make sure that doesn't change # when changing the value: el.setIdAttribute("spam") self.confirm(len(el.attributes) == 1 and el.attributes["spam"].value == "bam" and el.attributes["spam"].nodeValue == "bam" and el.getAttribute("spam") == "bam" and el.getAttributeNode("spam").isId) el.attributes["spam"] = "ham" self.confirm(len(el.attributes) == 1 and el.attributes["spam"].value == "ham" and el.attributes["spam"].nodeValue == "ham" and el.getAttribute("spam") == "ham" and el.attributes["spam"].isId) el.setAttribute("spam2", "bam") self.confirm(len(el.attributes) == 2 and el.attributes["spam"].value == "ham" and el.attributes["spam"].nodeValue == "ham" and el.getAttribute("spam") == "ham" and el.attributes["spam2"].value == "bam" and el.attributes["spam2"].nodeValue == "bam" and el.getAttribute("spam2") == "bam") el.attributes["spam2"] = "bam2" self.confirm(len(el.attributes) == 2 and el.attributes["spam"].value == "ham" and el.attributes["spam"].nodeValue == "ham" and el.getAttribute("spam") == "ham" and el.attributes["spam2"].value == "bam2" and el.attributes["spam2"].nodeValue == "bam2" and el.getAttribute("spam2") == "bam2") dom.unlink()
def testRemoveNamedItem(self): doc = parseString("<doc a=''/>") e = doc.documentElement attrs = e.attributes a1 = e.getAttributeNode("a") a2 = attrs.removeNamedItem("a") self.confirm(a1.isSameNode(a2)) self.assertRaises(xml.dom.NotFoundErr, attrs.removeNamedItem, "a")
def testRemoveNamedItemNS(self): doc = parseString("<doc xmlns:a='http://xml.python.org/' a:b=''/>") e = doc.documentElement attrs = e.attributes a1 = e.getAttributeNodeNS("http://xml.python.org/", "b") a2 = attrs.removeNamedItemNS("http://xml.python.org/", "b") self.confirm(a1.isSameNode(a2)) self.assertRaises(xml.dom.NotFoundErr, attrs.removeNamedItemNS, "http://xml.python.org/", "b")
def _setupCloneElement(self, deep): dom = parseString("<doc attr='value'><foo/></doc>") root = dom.documentElement clone = root.cloneNode(deep) self._testCloneElementCopiesAttributes( root, clone, "testCloneElement" + (deep and "Deep" or "Shallow")) # mutilate the original so shared data is detected root.tagName = root.nodeName = "MODIFIED" root.setAttribute("attr", "NEW VALUE") root.setAttribute("added", "VALUE") return dom, clone
def check_clone_attribute(self, deep, testName): doc = parseString("<doc attr='value'/>") attr = doc.documentElement.getAttributeNode("attr") self.assertNotEqual(attr, None) clone = attr.cloneNode(deep) self.confirm(not clone.isSameNode(attr)) self.confirm(not attr.isSameNode(clone)) self.confirm(clone.ownerElement is None, testName + ": ownerElement should be None") self.confirm(clone.ownerDocument.isSameNode(attr.ownerDocument), testName + ": ownerDocument does not match") self.confirm(clone.specified, testName + ": cloned attribute must have specified == True")
def testBug0777884(self): doc = parseString("<o>text</o>") text = doc.documentElement.childNodes[0] self.assertEqual(text.nodeType, Node.TEXT_NODE) # Should run quietly, doing nothing. text.normalize() doc.unlink()
def testBug1433694(self): doc = parseString("<o><i/>t</o>") node = doc.documentElement node.childNodes[1].nodeValue = "" node.normalize() self.confirm(node.childNodes[-1].nextSibling is None, "Final child's .nextSibling should be None")
def testSiblings(self): doc = parseString("<doc><?pi?>text?<elm/></doc>") root = doc.documentElement (pi, text, elm) = root.childNodes self.confirm(pi.nextSibling is text and pi.previousSibling is None and text.nextSibling is elm and text.previousSibling is pi and elm.nextSibling is None and elm.previousSibling is text, "testSiblings") doc.unlink()