我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用xml.dom.appendChild()。
def testLegalChildren(self): dom = Document() elem = dom.createElement('element') text = dom.createTextNode('text') self.assertRaises(xml.dom.HierarchyRequestErr, dom.appendChild, text) dom.appendChild(elem) self.assertRaises(xml.dom.HierarchyRequestErr, dom.insertBefore, text, elem) self.assertRaises(xml.dom.HierarchyRequestErr, dom.replaceChild, text, elem) nodemap = elem.attributes self.assertRaises(xml.dom.HierarchyRequestErr, nodemap.setNamedItem, text) self.assertRaises(xml.dom.HierarchyRequestErr, nodemap.setNamedItemNS, text) elem.appendChild(text) dom.unlink()
def testAddAttr(self): dom = Document() child = dom.appendChild(dom.createElement("abc")) child.setAttribute("def", "ghi") self.confirm(child.getAttribute("def") == "ghi") self.confirm(child.attributes["def"].value == "ghi") child.setAttribute("jkl", "mno") self.confirm(child.getAttribute("jkl") == "mno") self.confirm(child.attributes["jkl"].value == "mno") self.confirm(len(child.attributes) == 2) child.setAttribute("def", "newval") self.confirm(child.getAttribute("def") == "newval") self.confirm(child.attributes["def"].value == "newval") self.confirm(len(child.attributes) == 2) dom.unlink()
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 test_toprettyxml_with_adjacent_text_nodes(self): # see issue #4147, adjacent text nodes are indented normally dom = Document() elem = dom.createElement(u'elem') elem.appendChild(dom.createTextNode(u'TEXT')) elem.appendChild(dom.createTextNode(u'TEXT')) dom.appendChild(elem) decl = '<?xml version="1.0" ?>\n' self.assertEqual(dom.toprettyxml(), decl + '<elem>\n\tTEXT\n\tTEXT\n</elem>\n')
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 testAppendChildFragment(self): dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes() dom.documentElement.appendChild(frag) self.confirm(tuple(dom.documentElement.childNodes) == (orig, c1, c2, c3), "appendChild(<fragment>)") frag.unlink() dom.unlink()
def testNonZero(self): dom = parse(tstfile) self.confirm(dom)# should not be zero dom.appendChild(dom.createComment("foo")) self.confirm(not dom.childNodes[-1].childNodes) dom.unlink()
def testDeleteAttr(self): dom = Document() child = dom.appendChild(dom.createElement("abc")) self.confirm(len(child.attributes) == 0) child.setAttribute("def", "ghi") self.confirm(len(child.attributes) == 1) del child.attributes["def"] self.confirm(len(child.attributes) == 0) dom.unlink()
def testRemoveAttr(self): dom = Document() child = dom.appendChild(dom.createElement("abc")) child.setAttribute("def", "ghi") self.confirm(len(child.attributes) == 1) child.removeAttribute("def") self.confirm(len(child.attributes) == 0) dom.unlink()
def testRemoveAttrNS(self): dom = Document() child = dom.appendChild( dom.createElementNS("http://www.python.org", "python:abc")) child.setAttributeNS("http://www.w3.org", "xmlns:python", "http://www.python.org") child.setAttributeNS("http://www.python.org", "python:abcattr", "foo") self.confirm(len(child.attributes) == 2) child.removeAttributeNS("http://www.python.org", "abcattr") self.confirm(len(child.attributes) == 1) dom.unlink()
def testRemoveAttributeNode(self): dom = Document() child = dom.appendChild(dom.createElement("foo")) child.setAttribute("spam", "jam") self.confirm(len(child.attributes) == 1) node = child.getAttributeNode("spam") child.removeAttributeNode(node) self.confirm(len(child.attributes) == 0 and child.getAttributeNode("spam") is None) dom.unlink()
def testElementReprAndStrUnicode(self): dom = Document() el = dom.appendChild(dom.createElement("abc")) string1 = repr(el) string2 = str(el) self.confirm(string1 == string2) dom.unlink()
def testElementReprAndStrUnicodeNS(self): dom = Document() el = dom.appendChild( dom.createElementNS("http://www.slashdot.org", "slash:abc")) string1 = repr(el) string2 = str(el) self.confirm(string1 == string2) self.confirm("slash:abc" in string1) dom.unlink()
def testAttributeRepr(self): dom = Document() el = dom.appendChild(dom.createElement("abc")) node = el.setAttribute("abc", "def") self.confirm(str(node) == repr(node)) dom.unlink()
def test_toprettyxml_with_adjacent_text_nodes(self): # see issue #4147, adjacent text nodes are indented normally dom = Document() elem = dom.createElement('elem') elem.appendChild(dom.createTextNode('TEXT')) elem.appendChild(dom.createTextNode('TEXT')) dom.appendChild(elem) decl = '<?xml version="1.0" ?>\n' self.assertEqual(dom.toprettyxml(), decl + '<elem>\n\tTEXT\n\tTEXT\n</elem>\n')
def testTooManyDocumentElements(self): doc = parseString("<doc/>") elem = doc.createElement("extra") # Should raise an exception when adding an extra document element. self.assertRaises(xml.dom.HierarchyRequestErr, doc.appendChild, elem) elem.unlink() doc.unlink()