在Oracle 1.8中,JAXB出现了一个奇怪的问题。这是我在做什么:
(1)使用Apache HTTP组件从生产服务器下载XML响应
(2)从HTTP响应中解组并获取类Record
的适当实例
(3)将步骤(2)中的Record
实例编组到系统File
(4)当我尝试从步骤(3)的File
解组时,JAXB抛出错误
Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 1; Content is
not allowed in trailing section.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
代码如下:
//Get response from HTTP response to an InputStream
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//Unmarshall from inputstream to an instance of Record
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Record.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Record record = (Record) unmarshaller.unmarshal(is);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
//Marshall the content from record into a file called 'sample.xml'
JAXBContext jaxbContext = JAXBContext.newInstance(Record.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
File file = new File("sample.xml");
marshaller.marshal(record, file);
//Now when I try to unmarshall from sample.xml I get the exception shown above