Oracle JDK 1.8 JAXB无法从其创建的文件中解组

时间:2018-06-27 17:41:43

标签: jaxb xerces

在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

0 个答案:

没有答案