使用XOM xml库解析文件时,如何忽略DTD声明。我的文件包含以下行:
<?xml version="1.0"?>
<!DOCTYPE BlastOutput PUBLIC "-//NCBI//NCBI BlastOutput/EN" "NCBI_BlastOutput.dtd">
//rest of stuff here
当我尝试构建()我的文档时,我得到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;
}
}
我在这一行得到一个NullPointerException:
Element root = _document.getRootElement();
从源XML文件中删除DTD行后,我可以成功解析它,但这不是最终生产系统中的一个选项。
答案 0 :(得分:14)
首选解决方案是实现拦截DTD请求的EntityResolver,并将这些请求重定向到嵌入式副本。如果你
您可以通过设置相应的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);
答案 1 :(得分:-5)
根据他们的文档,这是解析文档而无需任何验证的方法。
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.");
}
如果您确实要验证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.");
}
注意现在可以抛出另一个异常:ValidityException