JAXBException:意外的元素(uri:“”,local:“workConfigRestWrapper”)。预期元素为< {} Config>,< {} MyMap>

时间:2012-11-16 19:45:10

标签: jaxb unmarshalling xml-binding

我需要使用xml绑定解组映射给出错误。

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:     公共类MyAdaptor扩展XmlAdapter&gt; {

@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;
}
}

客户代码:

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

错误讯息:

JAXBException:意外元素(uri:“”,local:“workConfigRestWrapper”)。预期元素为&lt; {} Config&gt;,&lt; {} MyMap&gt; javax.xml.bind.UnmarshalException:意外元素(uri:“”,local:“workConfigRestWrapper”)。预期元素为&lt; {} Config&gt;,&lt; {} MyMap&gt;     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)     在org.apache.xerces.parsers.AbstractSAXParser.startElement(未知来源)     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)     在org.apache.xerces.parsers.XML11Configuration.parse(未知来源)     在org.apache.xerces.parsers.XML11Configuration.parse(未知来源)     在org.apache.xerces.parsers.XMLParser.parse(未知来源)     在org.apache.xerces.parsers.AbstractSAXParser.parse(未知来源)     在org.apache.xerces.jaxp.SAXParserImpl $ JAXPSAXParser.parse(未知来源)     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)     在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)     在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)     在com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.execute(CustomerRemoteAgent.java:193)     在com.ge.dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.main(CustomerRemoteAgent.java:364)

2 个答案:

答案 0 :(得分:1)

我将客户端代码修改为:

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

我使用myMap而不是包装类。 :(

答案 1 :(得分:0)

尝试在MyMap.java类上放置注释@XmlType(propOrder = {})。 通过使用此类型,您可以按任何顺序读取文件的内容。

相关问题