尝试使用eclipselink moxy将一些XML拆开到POJO中时,我将获得一个FileNotFoundException
,其中它正在寻找文档的DTD作为相对路径。
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:UsersdeejayDocumentsworkspace-sts-3.0.0.RELEASEmoxy-testieee_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,或者不尝试解决它,那就太好了。
您可以从已禁用DTD支持的XMLStreamReader
中解散:
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声明,则可以执行以下操作:
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();