我正在尝试使用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();
}
答案 0 :(得分:0)
在对其使用SAX解析后,您无法解散InputSource
。要在getResult
上获取对象UnmarshallerHandler
。