當前位置: 首頁>>代碼示例>>Python>>正文


Python expat.ParseFile方法代碼示例

本文整理匯總了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) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:13,代碼來源:test_pyexpat.py

示例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) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:41,代碼來源:test_pyexpat.py

示例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) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:61,代碼來源:test_pyexpat.py


注:本文中的xml.parsers.expat.ParseFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。