Question

I am trying to parse my xml using SAX ,I want to use JaxB to build my map my element name and its value.I also want to override startElement and endElemnt but I have reached here until so far and I am getting unmarshal exception.Any help is appreciated!.

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();  
    }
Was it helpful?

Solution

You can't unmarshal the InputSource after you have used a SAX parse on it. To get the object call getResult on the UnmarshallerHandler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top