简体   繁体   中英

Java JAXB unmarshaller linked exception

Background: I am trying to parse XML into object using JAXB unmarshaller.

What I've done: I used JAXB itself to generate object classes and wrote some methods to unmarshal xml.

public void xmlParser() {
    try {
        Acquirer acquirer = (Acquirer) readXml(Constants.XML_PATH);
        System.out.println(acquirer.getDate());
    } catch (JAXBException | IOException e) {
        e.printStackTrace();
    }
}

/**
 * Initializes of JAXB Context and Unmarshaller.
 */
private static void createContext() {
    try {
        jaxbContext = JAXBContext.newInstance("com.test.xml.generated");
        unmarshaller = jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

/**
 * This reads the XML file from the resources in ClassPath.
 *
 * @param xmlFile XML file name as String with relative ClassPath
 * @return Unmarashalled XML file
 * @throws JAXBException
 * @throws IOException
 * @throws Exception
 */
public Object readXml(String xmlFile) throws JAXBException, IOException {
    if (jaxbContext == null) {
        createContext();
    }

    InputStream stream = getClass().getClassLoader().getResourceAsStream(xmlFile);
    BufferedInputStream buffredStream = new BufferedInputStream(stream);

***Error:***
    Object obj = unmarshaller.unmarshal(buffredStream);
    buffredStream.close();
    stream.close();
    return obj;

 }

Error is in Object obj.....

Exception:

javax.xml.bind.UnmarshalException - with linked exception:
[java.io.IOException: Stream closed]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:246)
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:125)

What I've managed to search: I used xml validator to validate xml and it seems fine. I also saw that someone suggested not to use InputStream and etc.. So I tried using File file = new File(); nothing. Moreover I tried to check auto generated object classes, but didn't find anything suspicious. @XmlElement and Root seems to be defined just fine.

PS I have xsd scheme of this xml (I generated all object classes using this xsd). I even used online tools to validate them both and everything seems right.

Constants.XML_PATH = "/Acquirer.xml";

Simply change:

InputStream stream = getClass().getClassLoader().getResourceAsStream(xmlFile);

on:

InputStream stream = getClass().getResourceAsStream(xmlFile);

Because when you use getClass().getClassLoader().getResourceAsStream(xmlFile) then it returns null (does not find the resource) and BufferedInputStream then throws the IOException when providing null instead of input stream instance into constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM