简体   繁体   中英

Unable to unmarshal xml-string to java object

Unmarshal xml-string to java-object gives the following error:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"AppServerResponse"). Expected elements are <{ http://www.coda.com/efinance/schemas/appserver }AppServerResponse>

This is the AppServerResponse Java-class

@XmlRootElement (name = "AppServerResponse", namespace = "http://www.coda.com/efinance/schemas/appserver")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AppServerResponse", namespace = "http://www.coda.com/efinance/schemas/appserver", propOrder = {
    "getEnvironment",
    "listUserCompanies",
    "softToHardDate"
})
public class AppServerResponse  extends ServiceResponse
{

    @XmlElement(name = "GetEnvironment", namespace = "http://www.coda.com/efinance/schemas/appserver")

    protected GetEnvironmentResponseVerb getEnvironment;
    @XmlElement(name = "ListUserCompanies", namespace = "http://www.coda.com/efinance/schemas/appserver")
    protected ListUserCompaniesResponseVerb listUserCompanies;
    @XmlElement(name = "SoftToHardDate", namespace = "http://www.coda.com/efinance/schemas/appserver")

    protected SoftToHardDateResponseVerb softToHardDate;

The code for the unmarshal funtion and the xml-message been unmarshaled:

String strResponse = "<AppServerResponse uri=\"http://www.coda.com/efinance/schemas/appserver\" version=\"2.0\" >"
                                + "<GetEnvironment>"
                                    + "<Response status=\"success\">"
                                        + "<Environment>"
                                            + "<CmpCode>TEST</CmpCode>"
                                            + "<CapCode>INTERFACE</CapCode>"
                                            + "<HomeCurr>EUR</HomeCurr>"
                                            + "<DateOrder>1</DateOrder>"
                                            + "<DateDisplay>1</DateDisplay>"
                                            + "<DateSep>/</DateSep>"
                                            + "<HomeCurrDps>2</HomeCurrDps>"
                                            + "<HomeCurrSymbol>€</HomeCurrSymbol>"
                                            + "<HomeCurrSymbolPos>before</HomeCurrSymbolPos>"
                                            + "<HomeCurrLinkType>no_link</HomeCurrLinkType>"
                                            + "<HomeCurrParent>EURO</HomeCurrParent>"
                                            + "<CurrentPeriod>2018/4</CurrentPeriod>"
                                            + "<CmpTimeStamp>11</CmpTimeStamp>"
                                            + "<CapTimeStamp>3</CapTimeStamp>"
                                            + "<UsrTimeStamp>12</UsrTimeStamp>"
                                            + "<ServerVersion>14.000.0014</ServerVersion>"
                                        + "</Environment>"
                                    + "</Response>"
                                + "</GetEnvironment>"
                            + "</AppServerResponse>";
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(false);
            DocumentBuilder db;

            db = dbf.newDocumentBuilder();

            Document docResponse = db.parse(new InputSource(new StringReader(strResponse)));

            writeXmlDocumentToXmlFile(docResponse);

            JAXBContext contextResponse = JAXBContext.newInstance(responseClass);
            Unmarshaller u = contextResponse.createUnmarshaller();

            returnValue = u.unmarshal(docResponse);

Your JAXB annotations specify that your elements are in the http://www.coda.com/efinance/schemas/appserver namespace, but the elements in your ressponse String are actually not in a namespace. It looks like you're intending to set the default namespace via

String strResponse = "<AppServerResponse uri=\"http://www.coda.com/efinance/schemas/appserver\"..."

But the uri attribute doesn't actually do that. Instead you need the xmlns attribute:

String strResponse = "<AppServerResponse xmlns=\"http://www.coda.com/efinance/schemas/appserver\"..."

Note the xmlns attribute name instead of uri .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM