【问题标题】:JAXB : unmarshalexception - with linked exception: [java.io.IOException: Stream closed]JAXB:unmarshalexception - 带有链接异常:[java.io.IOException: Stream closed]
【发布时间】: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();  
    }

【问题讨论】:

    标签: java xml jaxb xsd sax


    【解决方案1】:

    在对 InputSource 使用 SAX 解析之后,您无法解组它。在UnmarshallerHandler 上调用getResult 获取对象。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多