gpt4 book ai didi

java - 如何使用 org.xml.sax.ext.EntityResolver2resolveEntity 方法获取非空名称参数?

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

在 Java SAX 处理中,我无法从 org.xml.sax.ext.EntityResolver2解决实体方法。名称参数始终为 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)

最佳答案

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

关于java - 如何使用 org.xml.sax.ext.EntityResolver2resolveEntity 方法获取非空名称参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/392268/

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