我正在使用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;
}
}
}
答案 0 :(得分: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实际上也有一个targetnamespace(我没有发布)和 当从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();