在Java SAX处理中,我没有设法从中获取实体引用名称
org.xml.sax.ext.EntityResolver2
resolveEntity方法。即使文档声明null
。
'name' is never null when invoked by a SAX2 parser
有什么想法吗?
为了演示这个问题,我创建了一个Jython 2.5脚本。它打印(在我的盒子上):
Entity Resolver 2 enabled: True
Entity name: None
Jython 2.5脚本:
# Jython
from org.python.core.util import StringUtil
from jarray import array
# Java Standard Edition
from org.xml.sax import *
from org.xml.sax.ext import *
from org.xml.sax.helpers import *
from java.io import ByteArrayInputStream
xml = """\
<!DOCTYPE doc
[<!ENTITY entity SYSTEM "entity-file">
]>
<doc>&entity;</doc>
"""
def empty_source():
_source = InputSource()
byte_stream = ByteArrayInputStream(array([], "b"))
_source.setByteStream(byte_stream)
return _source
class Handler(EntityResolver2):
def getExternalSubset(self, name, baseURI):
return None
def resolveEntity(self, name, publicId, baseURI, systemId):
print "Entity name:", name
return empty_source()
reader = XMLReaderFactory.createXMLReader()
enabled = reader.getFeature("http://xml.org/sax/features/use-entity-resolver2")
print "Entity Resolver 2 enabled:", enabled
handler = Handler()
reader.setEntityResolver(handler)
bytes = StringUtil.toBytes(xml)
byte_stream = ByteArrayInputStream(bytes)
source = InputSource(byte_stream)
reader.parse(source)
答案 0 :(得分:0)
使用更新版本的Xerces(例如2.9.1),而不是与Java捆绑的“旧”版本。