繁体   English   中英

JAXB 忽略@XmlAttribute 注释

[英]JAXB ignores @XmlAttribute annotation

似乎 JAXB 忽略了@XmlAttribute注释并且不填充attributeNameattributeValue属性。 你能帮我解决这个问题吗?

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>

您的 model 定义中有一些错误: attrList属性的@XmlElement应设置为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>的子元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM