使用EclipseLink MOXy解组时忽略DTD [英] Ignoring DTDs when unmarshalling with EclipseLink MOXy

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

问题描述

当尝试使用EclipseLink MOXy将一些XML解组为POJO时,我得到一个 FileNotFoundException ,它正在寻找文档的DTD作为相对路径。

When trying to unmarshall some XML into a POJO using EclipseLink MOXy I'm getting a FileNotFoundException where it's looking for the document's DTD as a relative path.

Exception in thread "main" org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException
 - with linked exception:
[java.io.FileNotFoundException: C:\Users\deejay\Documents\workspace-sts-3.0.0.RELEASE\moxy-test\ieee_idams_exchange.dtd (The system cannot find the file specified)]
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:761)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:682)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:665)
    at com.mendeley.services.utility.EclipseLinkMarshaller.load(EclipseLinkMarshaller.java:29)
    at com.mendeley.MoxyTest.main(MoxyTest.java:31)

我按照这个例子,所以我不知道为什么它甚至需要一个DTD。如果我可以让它忽略DTD,或者不尝试解决它,那就太好了。

I'm providing "externalized metadata" as per this example, so I've no idea why it even needs a DTD. If I could get it to ignore the DTD, or not try and resolve it, that'd be great.

推荐答案

你可以从已禁用DTD支持的 XMLStreamReader 解组:

You could unmarshal from an XMLStreamReader that has DTD support disabled:

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

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

        XMLInputFactory xif = XMLInputFactory.newFactory();
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("input.xml"));

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Customer customer = (Customer) unmarshaller.unmarshal(xsr);
    }

}

额外

如果您想写DTD声明,您可以执行以下操作:

If you want to write the DTD declaration you could do the following:

    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
    xsw.writeDTD("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    marshaller.marshal(html, xsw);
    xsw.close();

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

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