JAXBException : unexpected element (uri:"", local:"workConfigRestWrapper"). Expected elements are <{}Config>,<{}MyMap>

StackOverflow https://stackoverflow.com/questions/13423366

  •  29-11-2021
  •  | 
  •  

Вопрос

I need to unmarshall map using xml binding is giving error.

MyMap.java:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "MyMap")
public class MyMap {
@XmlElement(name = "Config", required = true)
private final List<Config> config = new ArrayList<Config>();

public List<Config> getConfig() {
    return this.config;
}
}

MyAdaptor.java : public class MyAdaptor extends XmlAdapter> {

@Override
public MyMap marshal(Map<String,String> v) throws Exception {
    MyMap myMap = new MyMap();
    List<Config> aList = myMap.getConfig();
    for ( Map.Entry<String,String> e : v.entrySet() ) {
        aList.add(new Config(e.getKey(), e.getValue()));
    }
    return myMap;
}

@Override
public Map<String,String> unmarshal(MyMap v) throws Exception {
    Map<String,String> map = new HashMap<String,String>();
    for (Config e : v.getConfig()) {
        map.put(e.getKey(), e.getValue());
    }
    return map;
}
}

Config.java :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Config")
public class Config {

@XmlAttribute(name = "key", required = true)
private final String key;
@XmlAttribute(name = "value", required = true)
private final String value;

public Config(String key, String value) {
    this.key = key;
    this.value = value;
}

public Config() {
    this.key = null;
    this.value = null;
}

public String getKey() {
    return key;
}

public String getValue() {
    return value;
}
}

client code :

            String getConfigurationMethod = baseUrl + "getConfiguration";
            byte[] getConfigurationResponse = (byte[]) this
                .sendGetMethod(getConfigurationMethod);
            unmarshaller = this.getUnmarshaller(MyMap.class);
    reader = new StringReader(new String(getConfigurationResponse));
    MyMap myMap = (MyMap) unmarshaller.unmarshal(reader);

Error Message:

JAXBException : unexpected element (uri:"", local:"workConfigRestWrapper"). Expected elements are <{}Config>,<{}MyMap> javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"workConfigRestWrapper"). Expected elements are <{}Config>,<{}MyMap> 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 org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source) at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:217) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:189) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194) at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:193) at com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.main(CustomerRemoteAgent.java:364)

Это было полезно?

Решение

I modified the client code as:

    byte[] getConfigurationResponse = (byte[]) this.util
            .sendGetMethod(this.getConfigurationMethod);

    Unmarshaller unmarshaller = this.getUnmarshaller(**WorkConfigRestWrapper.class**);
    StringReader reader = new StringReader(new String(getConfigurationResponse));
    **WorkConfigRestWrapper wrk** = (WorkConfigRestWrapper) unmarshaller
            .unmarshal(reader);

I was using myMap instead of the wrapper class. :(

Другие советы

Try put annotation @XmlType(propOrder = {}) on the MyMap.java class. By using this type you can read the content of the file in any sequence.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top