在使用XOM xml库解析文件时,如何忽略DTD声明.我的文件有以下行:
<?xml version="1.0"?>
<!DOCTYPE BlastOutput PUBLIC "-//NCBI//NCBI BlastOutput/EN" "NCBI_BlastOutput.dtd">
//rest of stuff here
Run Code Online (Sandbox Code Playgroud)
当我尝试构建()我的文档时,我得到一个DTD文件的文件未发现异常.我知道我没有这个文件,我不关心它,所以如何在使用XOM时将其删除?
这是一段代码:
public BlastXMLParser(String filePath) {
Builder b = new Builder(false);
//not a good idea to have exception-throwing code in constructor
try {
_document = b.build(filePath);
} catch (ParsingException ex) {
Logger.getLogger(BlastXMLParser.class.getName()).log(Level.SEVERE,"err", ex);
} catch (IOException ex) {
//
}
private Elements getBlastReads() {
Element root = _document.getRootElement();
Elements rootChildren = root.getChildElements();
for (int i = 0; i < rootChildren.size(); i++) {
Element child = rootChildren.get(i);
if (child.getLocalName().equals("BlastOutput_iterations")) {
return child.getChildElements();
}
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我在这一行得到一个NullPointerException:
Element root = _document.getRootElement();
Run Code Online (Sandbox Code Playgroud)
从源XML文件中删除DTD行后,我可以成功解析它,但这不是最终生产系统中的一个选项.
Jör*_*ann 14
首选解决方案是实现一个EntityResolver,它拦截对DTD的请求并将这些请求重定向到嵌入式副本.如果你
您可以通过设置相应的SAX功能来禁用DTD的提取.在XOM中,这应该可以通过将XMLReader传递给Builder构造函数来实现,如下所示:
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
...
XMLReader xmlreader = XMLReaderFactory.createXMLReader();
xmlreader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Builder builder = new Builder(xmlreader);
Run Code Online (Sandbox Code Playgroud)
Ale*_*exR -6
根据他们的文档,这是在没有任何验证的情况下解析文档的方法。
try {
Builder parser = new Builder();
Document doc = parser.build("http://www.cafeconleche.org/");
}
catch (ParsingException ex) {
System.err.println("Cafe con Leche is malformed today. How embarrassing!");
}
catch (IOException ex) {
System.err.println("Could not connect to Cafe con Leche. The site may be down.");
}
Run Code Online (Sandbox Code Playgroud)
如果您确实想验证 XML 模式,则必须调用new Builder(true):
try {
Builder parser = new Builder(true);
Document doc = parser.build("http://www.cafeconleche.org/");
}
catch (ValidityException ex) {
System.err.println("Cafe con Leche is invalid today. (Somewhat embarrassing.)");
}
catch (ParsingException ex) {
System.err.println("Cafe con Leche is malformed today. (How embarrassing!)");
}
catch (IOException ex) {
System.err.println("Could not connect to Cafe con Leche. The site may be down.");
}
Run Code Online (Sandbox Code Playgroud)
请注意,现在可能会引发另一个异常:ValidityException