本文整理匯總了Python中xml.sax.expatreader.create_parser方法的典型用法代碼示例。如果您正苦於以下問題:Python expatreader.create_parser方法的具體用法?Python expatreader.create_parser怎麽用?Python expatreader.create_parser使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類xml.sax.expatreader
的用法示例。
在下文中一共展示了expatreader.create_parser方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_expat_dtdhandler
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_dtdhandler(self):
parser = create_parser()
handler = self.TestDTDHandler()
parser.setDTDHandler(handler)
parser.feed('<!DOCTYPE doc [\n')
parser.feed(' <!ENTITY img SYSTEM "expat.gif" NDATA GIF>\n')
parser.feed(' <!NOTATION GIF PUBLIC "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN">\n')
parser.feed(']>\n')
parser.feed('<doc></doc>')
parser.close()
self.assertEqual(handler._notations,
[("GIF", "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN", None)])
self.assertEqual(handler._entities, [("img", None, "expat.gif", "GIF")])
# ===== EntityResolver support
示例2: test_expat_entityresolver
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_entityresolver(self):
parser = create_parser()
parser.setEntityResolver(self.TestEntityResolver())
result = StringIO()
parser.setContentHandler(XMLGenerator(result))
parser.feed('<!DOCTYPE doc [\n')
parser.feed(' <!ENTITY test SYSTEM "whatever">\n')
parser.feed(']>\n')
parser.feed('<doc>&test;</doc>')
parser.close()
self.assertEqual(result.getvalue(), start +
"<doc><entity></entity></doc>")
# ===== Attributes support
示例3: test_expat_nsattrs_wattr
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_nsattrs_wattr(self):
parser = create_parser(1)
gather = self.AttrGatherer()
parser.setContentHandler(gather)
parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri)
parser.close()
attrs = gather._attrs
self.assertEqual(attrs.getLength(), 1)
self.assertEqual(attrs.getNames(), [(ns_uri, "attr")])
self.assertTrue((attrs.getQNames() == [] or
attrs.getQNames() == ["ns:attr"]))
self.assertEqual(len(attrs), 1)
self.assertTrue(attrs.has_key((ns_uri, "attr")))
self.assertEqual(attrs.get((ns_uri, "attr")), "val")
self.assertEqual(attrs.get((ns_uri, "attr"), 25), "val")
self.assertEqual(attrs.items(), [((ns_uri, "attr"), "val")])
self.assertEqual(attrs.values(), ["val"])
self.assertEqual(attrs.getValue((ns_uri, "attr")), "val")
self.assertEqual(attrs[(ns_uri, "attr")], "val")
# ===== InputSource support
示例4: test_expat_incremental_reset
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_incremental_reset(self):
result = StringIO()
xmlgen = XMLGenerator(result)
parser = create_parser()
parser.setContentHandler(xmlgen)
parser.feed("<doc>")
parser.feed("text")
result = StringIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
parser.reset()
parser.feed("<doc>")
parser.feed("text")
parser.feed("</doc>")
parser.close()
self.assertEqual(result.getvalue(), start + "<doc>text</doc>")
# ===== Locator support
示例5: test_expat_locator_withinfo_unicode
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_locator_withinfo_unicode(self):
fname = support.TESTFN_UNICODE
shutil.copyfile(TEST_XMLFILE, fname)
self.addCleanup(support.unlink, fname)
result = StringIO()
xmlgen = XMLGenerator(result)
parser = create_parser()
parser.setContentHandler(xmlgen)
parser.parse(fname)
self.assertEqual(parser.getSystemId(), fname)
self.assertEqual(parser.getPublicId(), None)
# ===========================================================================
#
# error reporting
#
# ===========================================================================
示例6: test_expat_entityresolver
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_entityresolver(self):
parser = create_parser()
parser.setEntityResolver(self.TestEntityResolver())
result = BytesIO()
parser.setContentHandler(XMLGenerator(result))
parser.feed('<!DOCTYPE doc [\n')
parser.feed(' <!ENTITY test SYSTEM "whatever">\n')
parser.feed(']>\n')
parser.feed('<doc>&test;</doc>')
parser.close()
self.assertEqual(result.getvalue(), start +
b"<doc><entity></entity></doc>")
# ===== Attributes support
示例7: test_expat_nsattrs_wattr
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_nsattrs_wattr(self):
parser = create_parser(1)
gather = self.AttrGatherer()
parser.setContentHandler(gather)
parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri)
parser.close()
attrs = gather._attrs
self.assertEqual(attrs.getLength(), 1)
self.assertEqual(attrs.getNames(), [(ns_uri, "attr")])
self.assertTrue((attrs.getQNames() == [] or
attrs.getQNames() == ["ns:attr"]))
self.assertEqual(len(attrs), 1)
self.assertIn((ns_uri, "attr"), attrs)
self.assertEqual(attrs.get((ns_uri, "attr")), "val")
self.assertEqual(attrs.get((ns_uri, "attr"), 25), "val")
self.assertEqual(list(attrs.items()), [((ns_uri, "attr"), "val")])
self.assertEqual(list(attrs.values()), ["val"])
self.assertEqual(attrs.getValue((ns_uri, "attr")), "val")
self.assertEqual(attrs[(ns_uri, "attr")], "val")
# ===== InputSource support
示例8: test_expat_locator_withinfo_nonascii
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_locator_withinfo_nonascii(self):
fname = support.TESTFN_UNICODE
shutil.copyfile(TEST_XMLFILE, fname)
self.addCleanup(support.unlink, fname)
result = BytesIO()
xmlgen = XMLGenerator(result)
parser = create_parser()
parser.setContentHandler(xmlgen)
parser.parse(fname)
self.assertEqual(parser.getSystemId(), fname)
self.assertEqual(parser.getPublicId(), None)
# ===========================================================================
#
# error reporting
#
# ===========================================================================
示例9: test_expat_binary_file
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_binary_file(self):
parser = create_parser()
result = StringIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
parser.parse(open(TEST_XMLFILE))
self.assertEqual(result.getvalue(), xml_test_out)
示例10: test_expat_file_unicode
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_file_unicode(self):
fname = support.TESTFN_UNICODE
shutil.copyfile(TEST_XMLFILE, fname)
self.addCleanup(support.unlink, fname)
parser = create_parser()
result = StringIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
parser.parse(open(fname))
self.assertEqual(result.getvalue(), xml_test_out)
# ===== DTDHandler support
示例11: test_expat_attrs_empty
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_attrs_empty(self):
parser = create_parser()
gather = self.AttrGatherer()
parser.setContentHandler(gather)
parser.feed("<doc/>")
parser.close()
self.verify_empty_attrs(gather._attrs)
示例12: test_expat_nsattrs_empty
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_nsattrs_empty(self):
parser = create_parser(1)
gather = self.AttrGatherer()
parser.setContentHandler(gather)
parser.feed("<doc/>")
parser.close()
self.verify_empty_nsattrs(gather._attrs)
示例13: test_expat_inpsource_filename
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_inpsource_filename(self):
parser = create_parser()
result = StringIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
parser.parse(TEST_XMLFILE)
self.assertEqual(result.getvalue(), xml_test_out)
示例14: test_expat_inpsource_sysid
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_inpsource_sysid(self):
parser = create_parser()
result = StringIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
parser.parse(InputSource(TEST_XMLFILE))
self.assertEqual(result.getvalue(), xml_test_out)
示例15: test_expat_inpsource_sysid_unicode
# 需要導入模塊: from xml.sax import expatreader [as 別名]
# 或者: from xml.sax.expatreader import create_parser [as 別名]
def test_expat_inpsource_sysid_unicode(self):
fname = support.TESTFN_UNICODE
shutil.copyfile(TEST_XMLFILE, fname)
self.addCleanup(support.unlink, fname)
parser = create_parser()
result = StringIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
parser.parse(InputSource(fname))
self.assertEqual(result.getvalue(), xml_test_out)