我正在尝试使用 SAX 解析我的 xml,我想使用 JaxB 来构建我的 map 、我的元素名称及其值。我还想覆盖 startElement 和 endElemnt,但到目前为止我已经到达这里,我正在解码异常。如有任何帮助,我们将不胜感激!
JAXBContext jaxbContext = JAXBContext.newInstance(my.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
UnmarshallerHandler unmarshallerHandler = jaxbUnmarshaller.getUnmarshallerHandler();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(unmarshallerHandler);
InputStream inputStream = this.getClass().getResourceAsStream("my.xml");
InputSource inputSource = new InputSource(inputStream);
xr.parse(inputSource);
JAXBElement element = (JAXBElement) jaxbUnmarshaller.unmarshal(inputSource);
//or if I unmarahall to specific class object still i get same exception.
} catch (JAXBException e) {
// some exception occured
e.printStackTrace();
}
最佳答案
在使用 SAX 解析后,您无法对 InputSource
进行解码。要获取对象,请在 UnmarshallerHandler
上调用 getResult
。
关于java - JAXB : unmarshalexception - with linked exception: [java. io.IOException:流已关闭],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22900440/