用户登录
用户注册

分享至

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

  • 作者: 逛窑子的小男孩
  • 来源: 51数据库
  • 2022-11-17

问题描述

如何使用 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
软件
前端设计
程序设计
Java相关