本文整理汇总了Python中xml.sax.xmlreader.InputSource方法的典型用法代码示例。如果您正苦于以下问题:Python xmlreader.InputSource方法的具体用法?Python xmlreader.InputSource怎么用?Python xmlreader.InputSource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.sax.xmlreader
的用法示例。
在下文中一共展示了xmlreader.InputSource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_expat_nsattrs_wattr
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [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
示例2: test_expat_nsattrs_wattr
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [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
示例3: test_expat_nsattrs_no_namespace
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def test_expat_nsattrs_no_namespace():
parser = make_parser()
parser.setFeature(handler.feature_namespaces, 1)
gather = AttrGatherer()
parser.setContentHandler(gather)
a_name = "id" ; a_val = "val"
parser.parse(StringIO("<doc %s='%s'/>" % (a_name, a_val) ))
attrs = gather._attrs
return attrs.getLength() == 1 and \
attrs.getNames() == [(None, a_name)] and \
attrs.getQNames() == [a_name] and \
len(attrs) == 1 and \
attrs.has_key((None, a_name)) and \
attrs.keys() == [(None, a_name)] and \
attrs.get((None, a_name)) == a_val and \
attrs.get((None, a_name), 25) == a_val and \
attrs.items() == [((None, a_name), a_val)] and \
attrs.values() == [a_val] and \
attrs.getValue((None, a_name)) == a_val and \
attrs[(None, a_name)] == a_val
# ===== InputSource support
示例4: __init__
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def __init__(self, namespaceHandling=0, bufsize=2**16-20):
xmlreader.IncrementalParser.__init__(self, bufsize)
self._source = xmlreader.InputSource()
self._parser = None
self._namespaces = namespaceHandling
self._lex_handler_prop = None
self._parsing = 0
self._entity_stack = []
self._external_ges = 1
self._interning = None
# XMLReader methods
示例5: parse
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def parse(self, source):
"Parse an XML document from a URL or an InputSource."
source = saxutils.prepare_input_source(source)
self._source = source
self.reset()
self._cont_handler.setDocumentLocator(ExpatLocator(self))
xmlreader.IncrementalParser.parse(self, source)
示例6: parse
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def parse(self, source):
"Parse an XML document from a URL or an InputSource."
source = saxutils.prepare_input_source(source)
self._source = source
try:
self.reset()
self._cont_handler.setDocumentLocator(ExpatLocator(self))
xmlreader.IncrementalParser.parse(self, source)
except:
# bpo-30264: Close the source on error to not leak resources:
# xml.sax.parse() doesn't give access to the underlying parser
# to the caller
self._close_source()
raise
示例7: test_parse_InputSource
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def test_parse_InputSource(self):
# accept data without declared but with explicitly specified encoding
make_xml_file(self.data, 'iso-8859-1', None)
with io.open(TESTFN, 'rb') as f:
input = InputSource()
input.setByteStream(f)
input.setEncoding('iso-8859-1')
self.check_parse(input)
示例8: test_byte_stream
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def test_byte_stream(self):
# If the source is an InputSource that does not have a character
# stream but does have a byte stream, use the byte stream.
src = InputSource(self.file)
src.setByteStream(self.make_byte_stream())
prep = prepare_input_source(src)
self.assertIsNone(prep.getCharacterStream())
self.checkContent(prep.getByteStream(),
b"This is a byte stream.")
示例9: test_system_id
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def test_system_id(self):
# If the source is an InputSource that has neither a character
# stream nor a byte stream, open the system ID.
src = InputSource(self.file)
prep = prepare_input_source(src)
self.assertIsNone(prep.getCharacterStream())
self.checkContent(prep.getByteStream(),
b"This was read from a file.")
示例10: test_expat_inpsource_sysid
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [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)
示例11: test_expat_inpsource_sysid_unicode
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [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)
示例12: test_expat_inpsource_byte_stream
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def test_expat_inpsource_byte_stream(self):
parser = create_parser()
result = StringIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
inpsrc = InputSource()
inpsrc.setByteStream(open(TEST_XMLFILE))
parser.parse(inpsrc)
self.assertEqual(result.getvalue(), xml_test_out)
# ===== IncrementalParser support
示例13: test_expat_inpsource_location
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def test_expat_inpsource_location(self):
parser = create_parser()
parser.setContentHandler(ContentHandler()) # do nothing
source = InputSource()
source.setByteStream(StringIO("<foo bar foobar>")) #ill-formed
name = "a file name"
source.setSystemId(name)
try:
parser.parse(source)
self.fail()
except SAXException, e:
self.assertEqual(e.getSystemId(), name)
示例14: resolveEntity
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def resolveEntity(self, publicId, systemId):
inpsrc = InputSource()
inpsrc.setByteStream(StringIO("<entity/>"))
return inpsrc
示例15: test_expat_inpsource_stream
# 需要导入模块: from xml.sax import xmlreader [as 别名]
# 或者: from xml.sax.xmlreader import InputSource [as 别名]
def test_expat_inpsource_stream(self):
parser = create_parser()
result = StringIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
inpsrc = InputSource()
inpsrc.setByteStream(open(TEST_XMLFILE))
parser.parse(inpsrc)
self.assertEqual(result.getvalue(), xml_test_out)
# ===== IncrementalParser support