【问题标题】:Single String object unmarshal gives UnmarshalException单个字符串对象解组给出 UnmarshalException
【发布时间】:2015-06-15 18:45:18
【问题描述】:

我正在尝试解组一个简单的 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)

【问题讨论】:

    标签: java xml jaxb unmarshalling


    【解决方案1】:

    这样试试:

    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?

    【讨论】:

      【解决方案2】:

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

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

      关于注解,看看这个:convert xml to java object using jaxb (unmarshal)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-22
        • 1970-01-01
        • 2019-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多