我们从Python开源项目中,提取了以下22个代码示例,用于说明如何使用xml.dom.pulldom.SAX2DOM。
def test_sax_to_pulldom(self): tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>') handler = pulldom.SAX2DOM() sax.saxify(tree, handler) dom = handler.document self.assertEqual('a', dom.firstChild.localName) self.assertEqual('blaA', dom.firstChild.namespaceURI) children = dom.firstChild.childNodes self.assertEqual('ab', children[0].nodeValue) self.assertEqual('blaB', children[1].namespaceURI) self.assertEqual('ba', children[2].nodeValue)
def parse_lxml_dom(tree): handler = SAX2DOM() lxml.sax.saxify(tree, handler) return handler.document
def testSAX2DOM(self): from xml.dom import pulldom sax2dom = pulldom.SAX2DOM() sax2dom.startDocument() sax2dom.startElement("doc", {}) sax2dom.characters("text") sax2dom.startElement("subelm", {}) sax2dom.characters("text") sax2dom.endElement("subelm") sax2dom.characters("text") sax2dom.endElement("doc") sax2dom.endDocument() doc = sax2dom.document root = doc.documentElement (text1, elm1, text2) = root.childNodes text3 = elm1.childNodes[0] self.confirm(text1.previousSibling is None and text1.nextSibling is elm1 and elm1.previousSibling is text1 and elm1.nextSibling is text2 and text2.previousSibling is elm1 and text2.nextSibling is None and text3.previousSibling is None and text3.nextSibling is None, "testSAX2DOM - siblings") self.confirm(root.parentNode is doc and text1.parentNode is root and elm1.parentNode is root and text2.parentNode is root and text3.parentNode is elm1, "testSAX2DOM - parents") doc.unlink()
def test_sax2dom_fail(self): """SAX2DOM can"t handle a PI before the root element.""" pd = SAX2DOMTestHelper(None, SAXExerciser(), 12) self._test_thorough(pd)
def test_thorough_sax2dom(self): """Test some of the hard-to-reach parts of SAX2DOM.""" pd = SAX2DOMTestHelper(None, SAX2DOMExerciser(), 12) self._test_thorough(pd, False)
def reset(self): self.pulldom = pulldom.SAX2DOM() # This content handler relies on namespace support self.parser.setFeature(xml.sax.handler.feature_namespaces, 1) self.parser.setContentHandler(self.pulldom)
def test_basic(self): """Ensure SAX2DOM can parse from a stream.""" with io.StringIO(SMALL_SAMPLE) as fin: sd = SAX2DOMTestHelper(fin, xml.sax.make_parser(), len(SMALL_SAMPLE)) for evt, node in sd: if evt == pulldom.START_ELEMENT and node.tagName == "html": break # Because the buffer is the same length as the XML, all the # nodes should have been parsed and added: self.assertGreater(len(node.childNodes), 0)
def testSAX2DOM(self): """Ensure SAX2DOM expands nodes as expected.""" sax2dom = pulldom.SAX2DOM() sax2dom.startDocument() sax2dom.startElement("doc", {}) sax2dom.characters("text") sax2dom.startElement("subelm", {}) sax2dom.characters("text") sax2dom.endElement("subelm") sax2dom.characters("text") sax2dom.endElement("doc") sax2dom.endDocument() doc = sax2dom.document root = doc.documentElement (text1, elm1, text2) = root.childNodes text3 = elm1.childNodes[0] self.assertIsNone(text1.previousSibling) self.assertIs(text1.nextSibling, elm1) self.assertIs(elm1.previousSibling, text1) self.assertIs(elm1.nextSibling, text2) self.assertIs(text2.previousSibling, elm1) self.assertIsNone(text2.nextSibling) self.assertIsNone(text3.previousSibling) self.assertIsNone(text3.nextSibling) self.assertIs(root.parentNode, doc) self.assertIs(text1.parentNode, root) self.assertIs(elm1.parentNode, root) self.assertIs(text2.parentNode, root) self.assertIs(text3.parentNode, elm1) doc.unlink()