本文整理汇总了Python中xml.parsers.expat.ParserCreate方法的典型用法代码示例。如果您正苦于以下问题:Python expat.ParserCreate方法的具体用法?Python expat.ParserCreate怎么用?Python expat.ParserCreate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.parsers.expat
的用法示例。
在下文中一共展示了expat.ParserCreate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: loadXML
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def loadXML(self, fileName):
"""
Loads the crawler parameters from an XML file.
@param fileName The file from where is loaded the crawler data
"""
self._parser = expat.ParserCreate("UTF-8")
self._parser.StartElementHandler = self.__start_element
self._parser.EndElementHandler = self.__end_element
self._parser.CharacterDataHandler = self.__char_data
self._parser.returns_unicode = False
f = None
try:
f = open(fileName)
content = f.read()
self.__feed(content.replace("\n", ""))
finally:
if f is not None:
f.close()
示例2: test_parse_only_xml_data
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def test_parse_only_xml_data(self):
# http://python.org/sf/1296433
#
xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * 1025)
# this one doesn't crash
#xml = "<?xml version='1.0'?><s>%s</s>" % ('a' * 10000)
class SpecificException(Exception):
pass
def handler(text):
raise SpecificException
parser = expat.ParserCreate()
parser.CharacterDataHandler = handler
# https://github.com/IronLanguages/ironpython2/issues/464
if sys.platform == 'cli':
self.assertRaises(Exception, parser.Parse, xml, True)
else:
self.assertRaises(Exception, parser.Parse, xml)
示例3: test_unchanged_size
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def test_unchanged_size(self):
xml1 = ("<?xml version='1.0' encoding='iso8859'?><s>%s" % ('a' * 512))
xml2 = 'a'*512 + '</s>'
parser = expat.ParserCreate()
parser.CharacterDataHandler = self.counting_handler
parser.buffer_size = 512
parser.buffer_text = 1
# Feed 512 bytes of character data: the handler should be called
# once.
self.n = 0
parser.Parse(xml1)
self.assertEqual(self.n, 1)
# Reassign to buffer_size, but assign the same size.
parser.buffer_size = parser.buffer_size
self.assertEqual(self.n, 1)
# Try parsing rest of the document
parser.Parse(xml2)
self.assertEqual(self.n, 2)
示例4: test_ignore_use_foreign_dtd
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def test_ignore_use_foreign_dtd(self):
"""
If UseForeignDTD is passed True and a document with an external
entity reference is parsed, ExternalEntityRefHandler is called with
the public and system ids from the document.
"""
handler_call_args = []
def resolve_entity(context, base, system_id, public_id):
handler_call_args.append((public_id, system_id))
return 1
parser = expat.ParserCreate()
parser.UseForeignDTD(True)
parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS)
parser.ExternalEntityRefHandler = resolve_entity
parser.Parse(
"<?xml version='1.0'?><!DOCTYPE foo PUBLIC 'bar' 'baz'><element/>")
self.assertEqual(handler_call_args, [("bar", "baz")])
示例5: createParser
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def createParser(self):
"""Create a new parser object."""
return expat.ParserCreate()
示例6: reset
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def reset(self):
if self._namespaces:
self._parser = expat.ParserCreate(self._source.getEncoding(), " ",
intern=self._interning)
self._parser.namespace_prefixes = 1
self._parser.StartElementHandler = self.start_element_ns
self._parser.EndElementHandler = self.end_element_ns
else:
self._parser = expat.ParserCreate(self._source.getEncoding(),
intern = self._interning)
self._parser.StartElementHandler = self.start_element
self._parser.EndElementHandler = self.end_element
self._reset_cont_handler()
self._parser.UnparsedEntityDeclHandler = self.unparsed_entity_decl
self._parser.NotationDeclHandler = self.notation_decl
self._parser.StartNamespaceDeclHandler = self.start_namespace_decl
self._parser.EndNamespaceDeclHandler = self.end_namespace_decl
self._decl_handler_prop = None
if self._lex_handler_prop:
self._reset_lex_handler_prop()
# self._parser.DefaultHandler =
# self._parser.DefaultHandlerExpand =
# self._parser.NotStandaloneHandler =
self._parser.ExternalEntityRefHandler = self.external_entity_ref
try:
self._parser.SkippedEntityHandler = self.skipped_entity_handler
except AttributeError:
# This pyexpat does not support SkippedEntity
pass
self._parser.SetParamEntityParsing(
expat.XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE)
self._parsing = 0
self._entity_stack = []
# Locator methods
示例7: __init__
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def __init__(self, html=0, target=None):
try:
from xml.parsers import expat
except ImportError:
raise ImportError(
"No module named expat; use SimpleXMLTreeBuilder instead"
)
self._parser = parser = expat.ParserCreate(None, "}")
if target is None:
target = TreeBuilder()
self._target = target
self._names = {} # name memo cache
# callbacks
parser.DefaultHandlerExpand = self._default
parser.StartElementHandler = self._start
parser.EndElementHandler = self._end
parser.CharacterDataHandler = self._data
# let expat do the buffering, if supported
try:
self._parser.buffer_text = 1
except AttributeError:
pass
# use new-style attribute handling, if supported
try:
self._parser.ordered_attributes = 1
self._parser.specified_attributes = 1
parser.StartElementHandler = self._start_list
except AttributeError:
pass
encoding = None
if not parser.returns_unicode:
encoding = "utf-8"
# target.xml(encoding, None)
self._doctype = None
self.entity = {}
示例8: __init__
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def __init__(self, target):
self._parser = parser = expat.ParserCreate(None, None)
self._target = target
parser.StartElementHandler = target.start
parser.EndElementHandler = target.end
parser.CharacterDataHandler = target.data
encoding = None
target.xml(encoding, None)
示例9: __init__
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def __init__(self):
self._parser = expat.ParserCreate()
self._parser.StartElementHandler = self.start_element
self._parser.EndElementHandler = self.end_element
self._parser.CharacterDataHandler = self.char_data
示例10: __init__
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def __init__(self):
""" Initializes the object"""
self._parser = expat.ParserCreate()
self._parser.StartElementHandler = self.start
self._parser.EndElementHandler = self.end
self._parser.CharacterDataHandler = self.data
self.hrefflag = 0
self.href = ''
self.status = 0
self.size = 0
self.dict = {}
self.sizes = {}
self.list = []
示例11: parse
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def parse(self, fileobj):
from xml.parsers.expat import ParserCreate
parser = ParserCreate()
parser.StartElementHandler = self.handleBeginElement
parser.EndElementHandler = self.handleEndElement
parser.CharacterDataHandler = self.handleData
parser.ParseFile(fileobj)
return self.root
示例12: __init__
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def __init__(self, target):
self._parser = parser = expat.ParserCreate(None, None)
self._target = target
parser.StartElementHandler = target.start
parser.EndElementHandler = target.end
parser.CharacterDataHandler = target.data
encoding = None
if not parser.returns_unicode:
encoding = "utf-8"
target.xml(encoding, None)
示例13: test_unicode
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def test_unicode(self):
# Try the parse again, this time producing Unicode output
out = self.Outputter()
parser = expat.ParserCreate(namespace_separator='!')
parser.returns_unicode = 1
for name in self.handler_names:
setattr(parser, name, getattr(out, name))
parser.Parse(data, 1)
op = out.out
self.assertEqual(op[0], 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'')
self.assertEqual(op[1], "Comment: u' comment data '")
self.assertEqual(op[2], "Notation declared: (u'notation', None, u'notation.jpeg', None)")
self.assertEqual(op[3], "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')")
self.assertEqual(op[4], "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}")
self.assertEqual(op[5], "NS decl: u'myns' u'http://www.python.org/namespace'")
self.assertEqual(op[6], "Start element: u'http://www.python.org/namespace!subelement' {}")
self.assertEqual(op[7], "Character data: u'Contents of subelements'")
self.assertEqual(op[8], "End element: u'http://www.python.org/namespace!subelement'")
self.assertEqual(op[9], "End of NS decl: u'myns'")
self.assertEqual(op[10], "Start element: u'sub2' {}")
self.assertEqual(op[11], 'Start of CDATA section')
self.assertEqual(op[12], "Character data: u'contents of CDATA section'")
self.assertEqual(op[13], 'End of CDATA section')
self.assertEqual(op[14], "End element: u'sub2'")
self.assertEqual(op[15], "External entity ref: (None, u'entity.file', None)")
self.assertEqual(op[16], "End element: u'root'")
示例14: test_parse_again
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def test_parse_again(self):
parser = expat.ParserCreate()
file = StringIO.StringIO(data)
parser.ParseFile(file)
# Issue 6676: ensure a meaningful exception is raised when attempting
# to parse more than one XML document per xmlparser instance,
# a limitation of the Expat library.
with self.assertRaises(expat.error) as cm:
parser.ParseFile(file)
self.assertEqual(expat.ErrorString(cm.exception.code),
expat.errors.XML_ERROR_FINISHED)
示例15: test_legal
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParserCreate [as 别名]
def test_legal(self):
# Tests that make sure we get errors when the namespace_separator value
# is illegal, and that we don't for good values:
expat.ParserCreate()
expat.ParserCreate(namespace_separator=None)
expat.ParserCreate(namespace_separator=' ')