使用 JAXB 的具有属性和内容的 XML 元素

2022-01-19 00:00:00 xml java jaxb

如何使用 JAXB 生成以下 XML?

How can I generate the following XML using JAXB?

<sport type="" gender="">
    sport description
</sport>

推荐答案

@XmlAttribute注释类型和性别属性,用@XmlValue注释描述属性:

Annotate type and gender properties with @XmlAttribute and the description property with @XmlValue:

package org.example.sport;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Sport {

    @XmlAttribute
    protected String type;

    @XmlAttribute
    protected String gender;

    @XmlValue;
    protected String description;

}

更多信息

  • http://bdoughan.blogspot.com/2011/06/jaxb-and-complex-types-with-simple.html

相关文章