English 中文(简体)
我该如何使用org.xml.sax.ext.EntityResolver2 resolveEntity方法获取非空名称参数?
原标题:
  • 时间:2008-12-24 22:05:54
  •  标签:

In Java SAX processing, I dot not manage to obtain entity reference names from the org.xml.sax.ext.EntityResolver2 resolveEntity method. The name argument is always null even if the documentation states that 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)
问题回答

使用Xerces的更新版本(例如2.9.1),而不是与Java捆绑的“旧”版本。





相关问题
热门标签