package com.thb;

import jakarta.xml.bind.annotation.XmlType;
import jakarta.xml.bind.annotation.XmlValue;

@XmlType(name = "", propOrder = {"value"})
public class Price {

    @XmlValue
    public String value;
}

生成的XML Schema中,类被映射为simpleType:

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

  <xs:simpleType name="price">
    <xs:restriction base="xs:string"/>
  </xs:simpleType>
</xs:schema>
  • 如果除了被XmlValue注解的JavaBean属性以外,Java类还有其它的属性被映射为XML中的属性,那么该Java类将会被映射为XML Schema中的complexType,并且在complexType元素下面包含一个simpleContent。
    例如,下面的Java类Price 中属性value使用了XmlValue注解,此外,属性currency使用了XmlAttribute注解(会被映射到XML中的属性):
package com.thb;

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

@XmlRootElement
@XmlType(name = "", propOrder = {"value", "currency"})
public class Price {

    @XmlValue
    public String value;

    @XmlAttribute
    public String currency;
}

生成的XML Schema中,类被映射为complexType,并且它下面包含一个simpleContent:

<?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="price">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="currency" type="xs:string"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

原文地址:https://blog.csdn.net/panghuangang/article/details/134640163

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_2521.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注