【发布时间】:2014-05-18 23:44:12
【问题描述】:
我正在尝试使用 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();
}
【问题讨论】: