解析XML时忽略DTD [英] Ignoring DTD when parsing XML

查看:708
本文介绍了解析XML时忽略DTD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用XOM xml库解析文件时,如何忽略DTD声明。我的文件包含以下行:

How can I ignore the DTD declaration when parsing file with XOM xml library. My file has the following line :

<?xml version="1.0"?>
<!DOCTYPE BlastOutput PUBLIC "-//NCBI//NCBI BlastOutput/EN" "NCBI_BlastOutput.dtd">
//rest of stuff here 

当我尝试构建()我的文档时获取DTD文件的filenotfound异常。我知道我没有这个文件,我不关心它,所以如何在使用XOM时删除它?

And when I try to build() my document I get a filenotfound exception for the DTD file. I know I don't have this file and I don't care about it, so how can it be removed when using 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:

I get a NullPointerException at this line:

Element root = _document.getRootElement();

从源XML文件中删除DTD行我可以成功解析它,但这不是最终生产系统中的选项。

With the DTD line removed from the source XML file I can successfully parse it, but this is not an option in the final production system.

推荐答案

首选解决方案是实现 EntityResolver 拦截DTD请求并将这些请求重定向到嵌入式副本。如果您

The preferred solution would be to implement an EntityResolver that intercepts requests for the DTD and redirects these to an embedded copy. If you


  1. 无法访问DTD和

  2. 绝对确定您赢了'需要它(除了验证它还可能声明文档中使用的字符实体)和

  3. 您正在使用Xerces XML Parser实现

您可以通过设置相应的SAX功能来禁用DTD的提取。在XOM中,这应该可以通过传递 XMLReader 到这样的Builder构造函数:

you can disable fetching of DTD by setting the corresponding SAX feature. In XOM this should be possible by passing an XMLReader to the Builder constructor like this:

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);

这篇关于解析XML时忽略DTD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆