我试图使用SAX解析我的xml,我想使用JaxB来构建我的地图我的元素名称和它的值。我也想重写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();
}
最佳答案
在对InputSource
使用SAX解析后,您无法将其取消编组。要获取对象,请在getResult
上调用UnmarshallerHandler
。