当前位置: 首页 > 知识库问答 >
问题:

JAXBException:意外元素(URI:“”,本地:“WorkConfigRestWrapper”)。需要的元素是、

应和悦
2023-03-14

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

@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;
}
}
@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;
}
}
@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:“”,本地:“WorkConfigRestWrapper”)。需要的元素是<{}config>、<{}mymap>javax.xml.bind.unmarshalException:意外元素(URI:“”,local:“workconfigrestwrapper”)。在com.sun.xml.bind.v2.runtime.unmarshaller.unmarshallingContext.HandleEvent(unmarshallingContext.java:662)在com.sun.xml.bind.v2.runtime.unmarshaller.loader.Reporder(loader.java:258)在com.sun.xml.bind.v2.runtime.unmarshaller.loader.Reporder(loader.java:253)在2.runtime.unmarshaller.unmarshallingcontext$defaultRootLoader.childelement(UnmarshallingContext.java:1063)在com.sun.xml.bind.v2.runtime.unmarshaller.unmarshallingContext._startelement(UnmarshallingContext.java:498)在com.sun.xml.bind.v2.runtime.unmarshallingContext.startelement(UnmarshallingContext.java:480)在s.parsers.abstractsaxparser.startelement(来源未知)位于org.apache.xerces.impl.xmlnsdocumentscannerimpl.scanstartelement(未知)源码)在org.apache.xerces.impl.xmlnsdocumentscannerimpl$nscontentdispatcher.scanrootelementhook(未知源码)在org.apache.xerces.impl.xmldocumentfragmentscannerimpl$fragmentcontentdispatcher.dispatch(未知源码)在org.apache.xerces.impl.xmldocumentfragmentscannerimpl(未知源码)在org.apache.xerces.impl.xmldocumentfragmentscannerimpl来源未知)在org.apache.xerces.parsers.abstractsaxparser.parse(来源未知)在org.apache.xerces.jaxp.saxparserimpl$jaxpsaxparser.parse(来源未知)在com.sun.xml.bind.v2.runtime.unmarshaller.unmarshaller.unmarshaller.unmarshalle.unmarshal0(unmarshalleripl.java:217)在com.sun.xml.bind.v2.runtime.unmarshallerimpl.unmarshal(shallerimpl.java:137)位于javax.xml.bind.helpers.abstractunMarshallerimpl.u在com.ge.dsp.iworkremote.remoteagents.customerremoteagent.execute(customerremoteagent.java:193)在com.ge.dsp.iworkremote.remoteagents.customerremoteagent.main(customerremoteagent.java:364)

共有1个答案

韩淇
2023-03-14

我将客户端代码修改为:

    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而不是包装类。:(

 类似资料: