本文整理汇总了Python中xml.parsers.expat.ParseFile方法的典型用法代码示例。如果您正苦于以下问题:Python expat.ParseFile方法的具体用法?Python expat.ParseFile怎么用?Python expat.ParseFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.parsers.expat
的用法示例。
在下文中一共展示了expat.ParseFile方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parse_again
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParseFile [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)
示例2: test_parse_file
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParseFile [as 别名]
def test_parse_file(self):
# Try parsing a file
out = self.Outputter()
parser = expat.ParserCreate(namespace_separator='!')
parser.returns_unicode = 1
for name in self.handler_names:
setattr(parser, name, getattr(out, name))
file = StringIO.StringIO(data)
parser.ParseFile(file)
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'")
# Issue 4877: expat.ParseFile causes segfault on a closed file.
fp = open(test_support.TESTFN, 'wb')
try:
fp.close()
parser = expat.ParserCreate()
with self.assertRaises(ValueError):
parser.ParseFile(fp)
finally:
test_support.unlink(test_support.TESTFN)
示例3: test_parse_file
# 需要导入模块: from xml.parsers import expat [as 别名]
# 或者: from xml.parsers.expat import ParseFile [as 别名]
def test_parse_file(self):
# Try parsing a file
out = self.Outputter()
parser = expat.ParserCreate(namespace_separator='!')
parser.returns_unicode = 1
for name in self.handler_names:
setattr(parser, name, getattr(out, name))
file = StringIO.StringIO(data)
parser.ParseFile(file)
op = out.out
# https://github.com/IronLanguages/ironpython2/issues/464
if sys.platform == 'cli':
self.assertEqual(op[0], 'PI: \'xml-stylesheet\' \'href="stylesheet.css"\'')
self.assertEqual(op[1], "Comment: ' comment data '")
#self.assertEqual(op[2], "Notation declared: ('notation', None, 'notation.jpeg', None)")
#self.assertEqual(op[3], "Unparsed entity decl: ('unparsed_entity', None, 'entity.file', None, 'notation')")
self.assertEqual(op[2], "Start element: 'root' {'attr1': 'value1', 'attr2': u'value2\\u1f40'}")
self.assertEqual(op[3], "NS decl: 'myns' 'http://www.python.org/namespace'")
self.assertEqual(op[4], "Start element: 'http://www.python.org/namespace!subelement' {}")
self.assertEqual(op[5], "Character data: 'Contents of subelements'")
self.assertEqual(op[6], "End element: 'http://www.python.org/namespace!subelement'")
self.assertEqual(op[7], "End of NS decl: 'myns'")
self.assertEqual(op[8], "Start element: 'sub2' {}")
self.assertEqual(op[9], 'Start of CDATA section')
self.assertEqual(op[10], "Character data: 'contents of CDATA section'")
self.assertEqual(op[11], 'End of CDATA section')
self.assertEqual(op[12], "End element: 'sub2'")
#self.assertEqual(op[13], "External entity ref: (None, 'entity.file', None)")
self.assertEqual(op[13], "End element: 'root'")
else:
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'")
# Issue 4877: expat.ParseFile causes segfault on a closed file.
fp = open(test_support.TESTFN, 'wb')
try:
fp.close()
parser = expat.ParserCreate()
with self.assertRaises(ValueError):
parser.ParseFile(fp)
finally:
test_support.unlink(test_support.TESTFN)