Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单字符串对象unmarshal提供UnmarshaleException_Java_Xml_Jaxb_Unmarshalling - Fatal编程技术网

Java 单字符串对象unmarshal提供UnmarshaleException

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字符串

<?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);