简体   繁体   English

Java JAXB解组链接异常

[英]Java JAXB unmarshaller linked exception

Background: I am trying to parse XML into object using JAXB unmarshaller. 背景:我正在尝试使用JAXB解组器将XML解析为对象。

What I've done: I used JAXB itself to generate object classes and wrote some methods to unmarshal xml. 完成的工作:我使用JAXB本身来生成对象类,并编写了一些方法来解组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..... 对象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. 我已经设法搜索的内容:我使用xml验证程序来验证xml,这看起来还不错。 I also saw that someone suggested not to use InputStream and etc.. So I tried using File file = new File(); 我还看到有人建议不要使用InputStream等。因此,我尝试使用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. @XmlElement和Root似乎定义得很好。

PS I have xsd scheme of this xml (I generated all object classes using this xsd). PS我有此xml的xsd方案(我使用此xsd生成了所有对象类)。 I even used online tools to validate them both and everything seems right. 我什至使用在线工具来验证它们,一切似乎都正确。

Constants.XML_PATH = "/Acquirer.xml"; 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. 因为当您使用getClass().getClassLoader().getResourceAsStream(xmlFile)它将返回null (找不到资源),并且在向构造函数中提供null而不是输入流实例时, BufferedInputStream会引发IOException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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