残念ながら、xml.catalog.files を設定してもパーサー ファクトリには影響しません。もちろん、理想的にはそうすべきですが、リゾルバーを使用する唯一の方法は、SAX パーサーが使用するハンドラー内のカタログ リゾルバーに解決を委譲するメソッドを追加することです。
すでに SAX パーサーを使用している場合、それは非常に簡単です。
final CatalogResolver catalogResolver = new CatalogResolver();
DefaultHandler handler = new DefaultHandler() {
public InputSource resolveEntity (String publicId, String systemId) {
return catalogResolver.resolveEntity(publicId, systemId);
}
public void startElement(String namespaceURI, String lname, String qname,
Attributes attrs) {
// the stuff you'd normally do
}
...
};
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser saxParser = factory.newSAXParser();
String url = args.length == 0 ? "http://horstmann.com/index.html" : args[0];
saxParser.parse(new URL(url).openStream(), handler);
それ以外の場合は、独自のエンティティ リゾルバーを提供できるかどうかを判断する必要があります。javax.xml.parsers.DocumentBuilder を使用すると、それが可能になります。scala.xml.XML オブジェクトでは、サブターフュージを使用することはできませんが、使用できます。
val res = new com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver
val loader = new factory.XMLLoader[Elem] {
override def adapter = new parsing.NoBindingFactoryAdapter() {
override def resolveEntity(publicId: String, systemId: String) = {
res.resolveEntity(publicId, systemId)
}
}
}
val doc = loader.load(new URL("http://horstmann.com/index.html"))enter code here