Xsd jaxb将xml解组到对象返回空列表

Xsd jaxb将xml解组到对象返回空列表,xsd,jaxb,xjc,Xsd,Jaxb,Xjc,我正在使用xjc为这个XSD生成Java类: * * * *列表中允许以下类型的对象 *{@link Message.Key} * * */ 公共列表getKey(){ if(key==null){ key=newarraylist(); } 返回此.key; } /** *获取类型属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getType(){ 返回类型; } /** *设置类型属性的值。 * *@param值 *允许的对

我正在使用xjc为这个XSD生成Java类:

* * * *列表中允许以下类型的对象 *{@link Message.Key} * * */ 公共列表getKey(){ if(key==null){ key=newarraylist(); } 返回此.key; } /** *获取类型属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getType(){ 返回类型; } /** *设置类型属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void集合类型(字符串值){ this.type=值; } /** *获取routingType属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getRoutingType(){ 返回路由类型; } /** *设置routingType属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void setRoutingType(字符串值){ this.routingType=值; } /** *获取computerName属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getComputerName(){ 返回计算机名; } /** *设置computerName属性的值。 * *@param值 *允许的对象是 *{@link String} * */ public void setComputerName(字符串值){ this.computerName=值; } /** *获取用户名属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getUserName(){ 返回用户名; } /** *设置用户名属性的值。 * *@param值 *允许的对象是 *{@link String} * */ public void setUserName(字符串值){ this.userName=值; } /** *获取模块属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getModule(){ 返回模块; } /** *设置模块属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void setModule(字符串值){ 本模块=数值; } /** *匿名复杂类型的Java类。 * *以下架构片段指定此类中包含的预期内容。 * * *complexType> *simpleContent> *扩展基=”http://www.w3.org/2001/XMLSchema>字符串“> *属性名称=“名称”类型=”{http://www.w3.org/2001/XMLSchema}字符串“/> *属性名称=“类型”类型=”{http://www.w3.org/2001/XMLSchema}字符串“/> */分机> */simpleContent> */complexType> * * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name=),比例={ “价值” }) 公共静态类密钥{ @XmlValue 保护字符串值; @XmlAttribute 受保护的字符串名称; @XmlAttribute 保护字符串类型; /** *获取value属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getValue(){ 返回值; } /** *设置值属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void设置值(字符串值){ 这个值=值; } /** *获取name属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getName(){ 返回名称; } /** *设置name属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void集合名(字符串值){ this.name=值; } /** *获取类型属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getType(){ 返回类型; } /** *设置类型属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void集合类型(字符串值){ this.type=值; } } }
基于您在问题中发布的
消息
类,运行以下命令:

Unmarshaller u = JAXBContext.newInstance("generated").createUnmarshaller();

当我从您发布的XML模式生成新模型时,我也得到了正确的输出,并且我将创建
JAXBContext
的方式更改为:

Unmarshaller u = JAXBContext.newInstance("generated").createUnmarshaller();

更新

我的xsd实际上也有一个targetnamespace(我没有发布)和 当从xsd中删除它时,我得到了您得到的结果。你有 关于如何解决这个问题的提示

如果您的
schema
元素如下所示:

Unmarshaller u = JAXBContext.newInstance("generated").createUnmarshaller();

然后,XML文档必须声明该名称空间并相应地限定元素。它可能看起来像:


CB
01
既然您是从XML模式生成模型的,我建议您创建
JAXBContext//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.12.20 at 09:24:38 AM CET 
//


package generated;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence maxOccurs="unbounded" minOccurs="0">
 *         &lt;element name="key">
 *           &lt;complexType>
 *             &lt;simpleContent>
 *               &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
 *                 &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
 *                 &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
 *               &lt;/extension>
 *             &lt;/simpleContent>
 *           &lt;/complexType>
 *         &lt;/element>
 *       &lt;/sequence>
 *       &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
 *       &lt;attribute name="routingType" type="{http://www.w3.org/2001/XMLSchema}string" />
 *       &lt;attribute name="computerName" type="{http://www.w3.org/2001/XMLSchema}string" />
 *       &lt;attribute name="userName" type="{http://www.w3.org/2001/XMLSchema}string" />
 *       &lt;attribute name="module" type="{http://www.w3.org/2001/XMLSchema}string" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "key"
})
@XmlRootElement(name = "message")
public class Message {

    protected List<Message.Key> key;
    @XmlAttribute
    protected String type;
    @XmlAttribute
    protected String routingType;
    @XmlAttribute
    protected String computerName;
    @XmlAttribute
    protected String userName;
    @XmlAttribute
    protected String module;

    /**
     * Gets the value of the key property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the key property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getKey().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Message.Key }
     * 
     * 
     */
    public List<Message.Key> getKey() {
        if (key == null) {
            key = new ArrayList<Message.Key>();
        }
        return this.key;
    }

    /**
     * Gets the value of the type property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getType() {
        return type;
    }

    /**
     * Sets the value of the type property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setType(String value) {
        this.type = value;
    }

    /**
     * Gets the value of the routingType property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getRoutingType() {
        return routingType;
    }

    /**
     * Sets the value of the routingType property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setRoutingType(String value) {
        this.routingType = value;
    }

    /**
     * Gets the value of the computerName property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getComputerName() {
        return computerName;
    }

    /**
     * Sets the value of the computerName property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setComputerName(String value) {
        this.computerName = value;
    }

    /**
     * Gets the value of the userName property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Sets the value of the userName property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUserName(String value) {
        this.userName = value;
    }

    /**
     * Gets the value of the module property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getModule() {
        return module;
    }

    /**
     * Sets the value of the module property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setModule(String value) {
        this.module = value;
    }


    /**
     * <p>Java class for anonymous complex type.
     * 
     * <p>The following schema fragment specifies the expected content contained within this class.
     * 
     * <pre>
     * &lt;complexType>
     *   &lt;simpleContent>
     *     &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
     *       &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
     *       &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
     *     &lt;/extension>
     *   &lt;/simpleContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "value"
    })
    public static class Key {

        @XmlValue
        protected String value;
        @XmlAttribute
        protected String name;
        @XmlAttribute
        protected String type;

        /**
         * Gets the value of the value property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getValue() {
            return value;
        }

        /**
         * Sets the value of the value property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setValue(String value) {
            this.value = value;
        }

        /**
         * Gets the value of the name property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getName() {
            return name;
        }

        /**
         * Sets the value of the name property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setName(String value) {
            this.name = value;
        }

        /**
         * Gets the value of the type property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getType() {
            return type;
        }

        /**
         * Sets the value of the type property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setType(String value) {
            this.type = value;
        }

    }

}
package generated;

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        StreamSource source = new StreamSource("src/generated/input.xml");

        Unmarshaller u = JAXBContext.newInstance(Message.class).createUnmarshaller();
        JAXBElement<Message> jaxbObject = u.unmarshal(source, Message.class);
        Message message = jaxbObject.getValue();
        System.out.print(message.getKey().size()); // list is empty
    }

}
2
Unmarshaller u = JAXBContext.newInstance("generated").createUnmarshaller();
Unmarshaller u = JAXBContext.newInstance("generated").createUnmarshaller();