单字符串对象unmarshal给出UnmarshalException

时间:2015-04-10 06:29:12

标签: java xml jaxb unmarshalling

我正在尝试解组一个简单的xml字符串。

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

这是我的代码。

             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();
                }

可能这是最简单的解组练习,但我得到了跟随错误。

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)

2 个答案:

答案 0 :(得分:0)

尝试这种方式:

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();
}

更多示例:How to parse a String containing XML in Java and retrieve the value of the root node?

答案 1 :(得分:0)

我看到你在RateQuoteError对象上使用了unmarshalled字符串。你的RateQuoteError看起来怎么样?您实际上可以使用JAXB将其直接解组到您的对象,并对您的RateQuoteError对象进行正确的注释:

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

对于注释,请看一下:convert xml to java object using jaxb (unmarshal)