1

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

<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">    
<xs:element name="message">
<xs:complexType>
  <xs:sequence maxOccurs="unbounded" minOccurs="0">
    <xs:element name="key">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute type="xs:string" name="name" use="optional"/>
            <xs:attribute type="xs:string" name="type" use="optional"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
  <xs:attribute type="xs:string" name="type"/>
  <xs:attribute type="xs:string" name="routingType"/>
  <xs:attribute type="xs:string" name="computerName"/>
  <xs:attribute type="xs:string" name="userName"/>
  <xs:attribute type="xs:string" name="module"/>
</xs:complexType>
</xs:element>
</xs:schema>

我的问题是解组以下 XML:

<message type="MSG_TYPE_TEST" routingType="routed" computerName="PRO31218S82-002" userName="SYSTEM" module="myService">
    <key name="HANDLER" type="String">CB</key>
   <key name="STAND" type="String">01</key>
</message>

进行解组的 Java src 如下所示:

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

message.geKey() 返回一个空列表,而 message.getType()、message.getRoutingType() 和其他属性 getter 返回预期值。
我究竟做错了什么?

Generated (by xjc) Message 类如下所示:

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

    }

}
4

1 回答 1

1

根据Message您在问题中发布的课程,运行以下命令:

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

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

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

更新

我的 xsd 实际上也有一个目标命名空间(我没有发布),当从 xsd 中删除它时,我得到了你得到的结果。你有关于如何解决这个问题的提示吗?

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

<xs:schema 
    elementFormDefault="qualified" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.com/foo">

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

<message 
    xmlns="http://www.example.com/foo"
    type="MSG_TYPE_TEST" 
    routingType="routed" 
    computerName="PRO31218S82-002" 
    userName="SYSTEM" module="myService">
    <key name="HANDLER" type="String">CB</key>
    <key name="STAND" type="String">01</key>
 </message>

然后,由于您从 XML 模式生成模型,我建议您JAXBContext在生成的包名称上创建您的模型。这将确保从类@XmlSchema上的注释中获取模式元数据package-info

Unmarshaller u = JAXBContext.newInstance("generated").createUnmarshaller();
于 2012-12-20T10:06:54.733 回答