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>
相关推荐
杨丰玮41820 小时前
从零手写Java飞机躲障碍游戏|Swing绘图、鼠标跟随、计时器碰撞检测实战(五)
java·python·游戏·游戏引擎·图形渲染·动画·贴图
莫得感情 o21 小时前
踩坑 - 压测三轮后 502:一个默认 10 的连接池如何拖垮整个服务
java
Escalating_xu21 小时前
【Linux线程】线程控制全解析:终止、join/detach、cancel、线程栈与 NPTL(下篇)
java·linux·运维
2602_9599609221 小时前
电商大厂Java面试实录:Spring Boot/JVM/Redis/Kafka/微服务/安全/测试全解析
java·jvm·spring boot·redis·面试题
吃饱了得干活21 小时前
限界上下文之后:微服务怎么拆、上下文怎么聊?
java·后端·架构
yngsqq1 天前
窗体快速取消
java·开发语言
岁月如歌77861 天前
一次讲透 Redis 缓存击穿、穿透、雪崩,从原理到实战解决方案
java·后端·架构
weixin_BYSJ19871 天前
springboot智慧公共交通系统---附源码51123
java·javascript·spring boot·python·django·flask·php
AI多Agent协作实战派1 天前
AI多Agent协作系统实战(四十六):改了十次规则,AI员工还是老样子——会话缓存的坑
java·spring·缓存
一知半解仙1 天前
当机器人学会泛化,而我在写Java:一名后端开发者的2026年8月21日观察手记
java·开发语言·机器人