XmlElement注解用在Java的列表属性上产生的效果

例如,下面的代码,XmlElement注解用在了Conditions类的conditionList属性的getter方法上,其中conditionList是一个列表类型:

复制代码
package com.thb;

import java.util.List;

import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;
import jakarta.xml.bind.annotation.XmlValue;

// 映射到xml中的conditions元素
@XmlRootElement(name = "conditions")
@XmlType(propOrder = {"conditionList"})
public class Conditions {

    /**
     * 查询条件列表
     */
    private List<Conditions.Condition> conditionList;

    @XmlElement(name = "condition")
    public List<Conditions.Condition> getconditionList(){
        return this.conditionList;
    }

    public void setCondition( List<Conditions.Condition> conditionList) {
        this.conditionList = conditionList;
    }


    @XmlType(propOrder = {"value"})
    public static class Condition {

        private String value;
        /**
         * 属性名称
         */
        private String name;
        /**
         * 属性类型
         */
        private String type;

        /**
         * 获取value的值
         */
        @XmlValue
        public String getValue() {
            return this.value;
        }

        /**
         * 给value赋值
         * @param value
         */
        public void setValue(String value) {
            this.value = value;
        }

        /**
         * 获取name的值
         */
        @XmlAttribute(name = "name")
        public String getName() {
            return this.name;
        }

        /**
         * 给name赋值
         * @param name
         */
        public void setName(String name) {
            this.name = name;
        }

        /**
         * 获取type的值
         */
        @XmlAttribute(name = "type")
        public String getType() {
            return this.type;
        }

        /**
         * 给type赋值
         * @param type
         */
        public void setType(String type) {
            this.type = type;
        }
    }
}

产生的XML Schema如下:

复制代码
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="conditions" type="conditions"/>

  <xs:complexType name="conditions">
    <xs:sequence>
      <xs:element name="condition" type="condition" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="condition">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="name" type="xs:string"/>
        <xs:attribute name="type" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>
相关推荐
金銀銅鐵6 分钟前
[Java] 如何理解 class 文件中字段的 access flags?
java·后端
小短腿的代码世界14 分钟前
Qt国际化深度解析:从源码到企业级多语言实践
java·数据库·qt
凌冰_24 分钟前
IDEA 集成Claude Code
java·ide
SXJR26 分钟前
Java中的Cross-Encoder模型解决方案
java·开发语言
彦为君35 分钟前
JavaSE-11-BIO/NIO/AIO(多人聊天室)
java·开发语言·python·ai·nio
计算机安禾42 分钟前
【c++面向对象编程】第43篇:可变参数模板(C++11):优雅处理不定长参数
java·开发语言·c++
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第69题】【JVM篇】第29题:GC Roots 有哪些?
java·开发语言·jvm·面试
William Dawson1 小时前
【通俗易懂!Spring四大核心注解源码解读:@Configuration、@ComponentScan、@Import、@EnableXXX实战】
java·后端·spring
Tigshop开源商城1 小时前
Tigshop 开源商城系统 JAVA v5.8.28 版本发布|『角色权限管理+店铺后台跳转逻辑』优化
java·开源商城系统·tigshop
彦为君1 小时前
JavaSE-10-并发编程(11个案例)
java·开发语言·python·ai·nio