我正在使用 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>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence maxOccurs="unbounded" minOccurs="0">
* <element name="key">
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </element>
* </sequence>
* <attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="routingType" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="computerName" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="userName" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="module" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </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>
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </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;
}
}
}