【发布时间】:2013-12-09 06:59:00
【问题描述】:
我正在尝试使用带有 SAX 的 JAXB 将 xml 解组为 java 对象。我在构造函数中写了这段代码。
logger.debug("Initializing jaxb...");
try {
jaxbContext = JAXBContext.newInstance(ProductInventory.class);
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
reader = saxParserFactory.newSAXParser().getXMLReader();
EntityResolver entityResolver = new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId,
String systemId) {
return new InputSource(new StringReader(""));
}
};
reader.setEntityResolver(entityResolver);
logger.debug("Successfully initialized jaxb.");
} catch (JAXBException | SAXException | ParserConfigurationException e) {
logger.error("Exception while initializing JAXB", e);
}
当多个请求到来时reader对象有什么问题吗?我是否必须始终获得reader 的参考?我的意思是对于每个解组操作,我都必须获得一个新的 xml 阅读器吗?
【问题讨论】: