java 如何使用 JAXB2.0 禁用 DTD 获取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9909465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:51:43  来源:igfitidea点击:

How to disable DTD fetching using JAXB2.0

javavalidationjaxbdtd

提问by Nick

I'm trying to use JAXB to unmashall some XML which I used xjc to create in the first place. I don't want to do any validation on the unmarshalling, but even though I have disabled the validation according to the JAXB documentation with u.setSchema(null);, but this hasn't prevented a FileNotFoundExceptionbeing thrown when it tries to run and can't find the schema.

我正在尝试使用 JAXB 来解散一些我首先使用 xjc 创建的 XML。我不想对解组进行任何验证,但即使我根据 JAXB 文档禁用了验证u.setSchema(null);,但这并没有阻止FileNotFoundException在它尝试运行并且找不到模式时抛出。

JAXBContext jc = JAXBContext.newInstance("blast");
Unmarshaller u = jc.createUnmarshaller();
u.setSchema(null);
return u.unmarshal(blast)

I've seen similar questions for disabling SAX parsing from validation by setting the apache property http://apache.org/xml/features/validation/schemato false, but I can't get the Unmarshaller to use my own sax parser.

我已经看到了致残SAX通过设置apache的财产验证解析类似的问题http://apache.org/xml/features/validation/schemafalse,但我不能让着Unmarshaller用自己的SAX解析器。

采纳答案by bdoughan

Below is sample code that demonstrates how to get a JAXB (JSR-222)implementation to use your SAX parser:

下面是演示如何获取JAXB (JSR-222)实现以使用 SAX 解析器的示例代码:

import java.io.FileReader;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXSource;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        XMLReader xmlReader = spf.newSAXParser().getXMLReader();
        InputSource inputSource = new InputSource(new FileReader("input.xml"));
        SAXSource source = new SAXSource(xmlReader, inputSource);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Foo foo = (Foo) unmarshaller.unmarshal(source);
        System.out.println(foo.getValue());
    }

}

回答by Renaud

Building on the answers from @blaise-doughan and @aerobiotic, here is a solution that worked for me:

基于@blaise-doughan 和@aerobiotic 的回答,这里有一个对我有用的解决方案:

import java.io.FileReader;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXSource;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class Demo2 {

    public static void main(String[] args) throws Exception {

        JAXBContext jc = JAXBContext.newInstance(MyBean.class);

        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        spf.setFeature("http://xml.org/sax/features/validation", false);

        XMLReader xmlReader = spf.newSAXParser().getXMLReader();
        InputSource inputSource = new InputSource(
                new FileReader("myfile.xml"));
        SAXSource source = new SAXSource(xmlReader, inputSource);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        MyBean foo = (MyBean) unmarshaller.unmarshal(source);
    }
}

回答by Philip Helger

You can create the Unmarshaller directly from a javax.xml.transform.sax.SAXSource.

您可以直接从 javax.xml.transform.sax.SAXSource 创建 Unmarshaller。

See the example on this page: http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/api/javax/xml/bind/Unmarshaller.html

请参阅此页面上的示例:http: //docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/api/javax/xml/bind/Unmarshaller.html

Than you "only" need to provide your own URIResolver to that SAXSource

比您“仅”需要向该 SAXSource 提供您自己的 URIResolver

回答by Ihor M.

Check out this resource https://java.net/projects/jaxb/lists/users/archive/2003-11/message/60It worked for me when best answer wasn't working.

查看此资源 https://java.net/projects/jaxb/lists/users/archive/2003-11/message/60当最佳答案不起作用时它对我有用。