0

I'm trying to unmarshall just a simple xml String.

<?xml version="1.0" ?><error>No images were found for the pro(s) entered.</error>

Here is my code.

             try{
                    jaxbContext = JAXBContext.newInstance(String.class);
                    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                    StringReader reader = new StringReader(output);
                    String error = unmarshaller.unmarshal(reader).toString();

                    RateQuoteError rateQuoteError = new RateQuoteError(error);
                    List<RateQuoteError> errorsList = new ArrayList<RateQuoteError>();
                    errorsList.add(rateQuoteError);
                    getPODResponse.setRateQuoteErrors(errorsList);
                }
                catch(UnmarshalException ume2)
                { 
                    ume2.printStackTrace();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

May be this is the easiest unmarshalling exercise but im getting following error.

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"error"). Expected elements are (none)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1063)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:498)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)
    at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:150)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:378)
Shashank Degloorkar
  • 3,151
  • 4
  • 32
  • 50

2 Answers2

0

Try in this way:

try{
    JAXBContext jaxbContext = JAXBContext.newInstance(String.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    StreamSource reader = new StreamSource(new StringReader(output));
    JAXBElement<String> error = unmarshaller.unmarshal(reader, String.class);

    RateQuoteError rateQuoteError = new RateQuoteError(error.getValue);
    List<RateQuoteError> errorsList = new ArrayList<RateQuoteError>();
    errorsList.add(rateQuoteError);
    getPODResponse.setRateQuoteErrors(errorsList);
} catch(UnmarshalException ume2) {
    ume2.printStackTrace();
} catch(Exception e) {
    e.printStackTrace();
}

More examples: How to parse a String containing XML in Java and retrieve the value of the root node?

Community
  • 1
  • 1
Krzysiek
  • 615
  • 8
  • 19
0

I see that you use the unmarshalled string on your RateQuoteError object. How does your RateQuoteError look? you can actually use JAXB to unmarshal it directly to your Object, with the right annotaions to your RateQuoteError object:

RateQuoteError rateQuoteError = (RateQuoteError) unmarshaller.unmarshal(reader);

For annotations, take a look at this: convert xml to java object using jaxb (unmarshal)

Community
  • 1
  • 1
jmcg
  • 1,547
  • 17
  • 22