JAXB 似乎忽略了 @XmlAttribute
注释并且没有填充 attributeName
和 attributeValue
属性。
你能帮我解决这个问题吗?
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Product>
<ProductCode>PRD191_5</ProductCode>
<AttrList>
<element Name="Toy Type" Value="Quadrocopter"/>
<element Name="Engine Type" Value="Electric Engine"/>
<element Name="Build Type" Value="Ready-To-Fly"/>
</AttrList>
</Product>
Java 类:
@XmlRootElement(name = "Product")
@XmlAccessorType(XmlAccessType.FIELD)
public class Product {
@XmlElement(name = "ProductCode")
private String productCode;
@XmlElement(name = "AttrList")
private List<Attribute> attrList = null;
}
@XmlRootElement(name = "element")
@XmlAccessorType(XmlAccessType.FIELD)
public class Attribute {
@XmlElement(name="element")
@Column
private String element;
@XmlAttribute(name = "Name", required=true)
@Column
private String attributeName;
@XmlAttribute(name = "Value", required=true)
@Column
private String attributeValue;
}
Maven 依赖:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
答案 0 :(得分:0)
您的模型定义中存在一些错误:@XmlElement
属性的 attrList
应设置为 element
(重复自身的元素),而包装元素的名称<AttrList>
应与 @XmlElementWrapper
注释一起传达:
@XmlRootElement(name = "Product")
@XmlAccessorType(XmlAccessType.FIELD)
public class Product {
@XmlElement(name = "ProductCode")
private String productCode;
@XmlElementWrapper(name = "AttrList")
@XmlElement(name = "element")
private List<Attribute> attrList = null;
}
您还应该删除 Java Bean 属性 Attribute#element
,因为它表示 <element>
标记的名为 <element>
的子元素。