Java 单字符串对象unmarshal提供UnmarshaleException
我只是想解组一个简单的xml字符串Java 单字符串对象unmarshal提供UnmarshaleException,java,xml,jaxb,unmarshalling,Java,Xml,Jaxb,Unmarshalling,我只是想解组一个简单的xml字符串 <?xml version="1.0" ?><error>No images were found for the pro(s) entered.</error> 试着这样做: try{ JAXBContext jaxbContext = JAXBContext.newInstance(String.class); Unmarshaller unmarshaller = jaxbContext.create
<?xml version="1.0" ?><error>No images were found for the pro(s) entered.</error>
试着这样做:
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();
}
更多示例:我看到您在RateQuoteError对象上使用了未经签名的字符串。你的RateQuoteError看起来怎么样?实际上,您可以使用JAXB将其直接解组到对象,并对RateQuoteError对象进行正确的注释: 有关注释,请查看以下内容:
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)
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();
}
RateQuoteError rateQuoteError = (RateQuoteError) unmarshaller.unmarshal(reader);