如何正确地使用SAX初始化JAXB?

时间:2014-08-11 10:36:34

标签: java xml jaxb sax

我有一个多线程访问它的类(类似于servlet的类)。有一个名为onEvent的方法,它接受InputStream并解组接收的输入流。

要解组,我在构造函数中初始化了JAXB和XMLReader,如下所示

try {
            jaxbContext = JAXBContext.newInstance(ProductInventory.class);
            jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            reader = saxParserFactory.newSAXParser().getXMLReader();
        } catch (JAXBException e) {
            logger.error("Exception while initializing JAXB", e);
        } catch (SAXException | ParserConfigurationException e) {
            logger.error("Exception while initializing sax xml reader", e);
        }

在onEvent方法中,我编写了以下代码

final InputSource inputSource = new InputSource(inputStream);
final SAXSource saxSource = new SAXSource(reader, inputSource);
SomeClass someClass = (SomeClass) jaxbUnmarshaller.unmarshal(saxSource);

初始化是否合适?我是否必须在新线程调用onEvent方法时重新初始化阅读器?

0 个答案:

没有答案