我想解析以下 XML 文档以解析其中的所有实体:
<!DOCTYPE doc SYSTEM 'mydoc.dtd'>
<doc>&title;</doc>
我的 EntityResolver 应该从数据库中获取具有给定系统 ID 的外部实体,然后进行解析,请参见下面的说明:
private static class MyEntityResolver
{
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException
{
// At this point, systemId is always absolutized to the current working directory,
// even though the XML document specified it as relative.
// E.g. "file:///H:/mydoc.dtd" instead of just "mydoc.dtd"
// Why??? How can I prevent this???
SgmlEntity entity = findEntityFromDatabase(systemId);
InputSource is = new InputSource(new ByteArrayInputStream(entity.getContents()));
is.setPublicId(publicId);
is.setSystemId(systemId);
return is;
}
}
我尝试使用 DOM (DocumentBuilder) 和 SAX (XMLReader),将实体解析器设置为 MyEntityResolver (ie ) ,setEntityResolver(new MyEntityResolver())
但总是被绝对化为当前工作目录。 systemId
MyEntityResolver#resolveEntity(String publicId, String systemId)
我也试过打电话setFeature("http://xml.org/sax/features/resolve-dtd-uris", false);
,但这没有任何帮助。
那么我怎样才能达到我想要的呢?
谢谢!