gpt4 book ai didi

python - 使用 python xml.sax 解析 XML 实体

转载 作者:行者123 更新时间:2023-12-01 06:10:03 25 4
gpt4 key购买 nike

使用 xml.sax 使用 python 解析 XML,但我的代码无法捕获实体。为什么skippedEntity()或resolveEntity()不报告以下内容:

import os
import cStringIO
import xml.sax
from xml.sax.handler import ContentHandler,EntityResolver,DTDHandler

#Class to parse and run test XML files
class TestHandler(ContentHandler,EntityResolver,DTDHandler):

#SAX handler - Entity resolver
def resolveEntity(self,publicID,systemID):
print "TestHandler.resolveEntity: %s %s" % (publicID,systemID)

def skippedEntity(self, name):
print "TestHandler.skippedEntity: %s" % (name)

def unparsedEntityDecl(self,publicID,systemID,ndata):
print "TestHandler.unparsedEntityDecl: %s %s" % (publicID,systemID)

def startElement(self,name,attrs):
# name = string.lower(name)
summary = '' + attrs.get('summary','')
arg = '' + attrs.get('arg','')
print 'TestHandler.startElement(), %s : %s (%s)' % (name,summary,arg)


def run(xml_string):
try:
parser = xml.sax.make_parser()
stream = cStringIO.StringIO(xml_string)

curHandler = TestHandler()
parser.setContentHandler(curHandler)
parser.setDTDHandler( curHandler )
parser.setEntityResolver( curHandler )

parser.parse(stream)
stream.close()
except (xml.sax.SAXParseException), e:
print "*** PARSER error: %s" % e;

def main():
try:
XML = "<!DOCTYPE page[ <!ENTITY num 'foo'> ]><test summary='step: &num;'>Entity: &not;</test>"
run(XML)
except Exception, e:
print 'FATAL ERROR: %s' % (str(e))

if __name__== '__main__':
main()

运行时,我看到的是:

 TestHandler.startElement(), step: foo ()
*** PARSER error: <unknown>:1:36: undefined entity

为什么我看不到#num; 的resolveEntity 打印内容?或 ¬?

的跳过条目打印

最佳答案

我认为resolveEntity 和skippedEntity 仅为外部DTD 调用。我通过修改 XML 使其工作。

XML = """<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE test SYSTEM "external.dtd" >
<test summary='step: &foo; &bar;'>Entity: &not;</test>
"""

external.dtd包含两个简单的实体声明。

<!ENTITY foo "bar">
<!ENTITY bar "foo">

此外,我摆脱了resolveEntity。

这输出 -

TestHandler.startElement(), test : step: bar foo ()
TestHandler.skippedEntity: not

希望这有帮助。

关于python - 使用 python xml.sax 解析 XML 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6349513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com