• JAXB:根据Java文件生成XML schema文件


    说明

    JAXB有个schemagen脚本,可以根据Java文件生成XML schema。这个工具在JAXB独立发布包中有,可以从官网下载JAXB的独立发布包:
    https://eclipse-ee4j.github.io/jaxb-ri/
    在这里插入图片描述

    示例

    使用schemagen -d 格式

    其中-d 指明了XML schema文件的输出路径
    如果要生成schema的Java文件没有引用外部的Java文件,可以使用这种方式。

    例如,根据下面这个Java类生成XML schema文件。

    package com.thb.server.register;
    
    import jakarta.xml.bind.annotation.XmlElement;
    import jakarta.xml.bind.annotation.XmlRootElement;
    
    // 使用了JAXB注解,映射到xml中的request元素
    @XmlRootElement(name = "request")
    @XmlType(propOrder = {"reqtype", "secret", "enterpriseAccount"})
    public class RegisterRequest {
    
        private String reqtype;
        private String secret;
        private String enterpriseAccount;
    
        // 使用了JAXB注解,映射到xml中的reqtype元素
        @XmlElement(name="reqtype", required = true)
        public String getReqtype() {
            return this.reqtype;
        }
    
        // 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值
        public void setReqtype(String reqtype) {
            this.reqtype = reqtype;
        }
    
        // 使用了JAXB注解,映射到xml中的secret元素
        @XmlElement(name="secret", required = true)
        public String getSecret() {
            return this.secret;
        }
    
        // 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值
        public void setSecret(String secret) {
            this.secret = secret;
        }
    
        // 使用了JAXB注解,映射到xml中的body元素
        @XmlElement(name = "body", required = true)
        public String getEnterpriseAccount() {
            return this.enterpriseAccount;
        }
    
        // 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值
        public void setEnterpriseAccount(String enterpriseAccount) {
            this.enterpriseAccount = enterpriseAccount;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    在cmd窗口下执行命令

    schemagen -d D:\temp\outschema D:\temp\eclipse-workspace\java_work\powe
    r-restful-webservice-server\src\main\java\com\thb\server\register\RegisterRequest.java
    
    • 1
    • 2

    在这里插入图片描述

    生成的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="request" type="registerRequest"/>
    
      <xs:complexType name="registerRequest">
        <xs:sequence>
          <xs:element name="reqtype" type="xs:string"/>
          <xs:element name="secret" type="xs:string"/>
          <xs:element name="body" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    使用schemagen -d -cp 格式

    其中-cp 指明了classpath,注意中不包含包名。
    如果要XML schema文件的Java文件引用了外部类,要使用-cp >指明寻找路径。

    例如,下面是要生成XML schema文件的Java文件,Java的属性seeContent引用了一外部类SeeContent:

    package com.thb.server.topology;
    
    import jakarta.xml.bind.annotation.XmlElement;
    import jakarta.xml.bind.annotation.XmlRootElement;
    import jakarta.xml.bind.annotation.XmlType;
    
    // 使用了JAXB注解,映射到xml中的request元素
    @XmlRootElement(name = "request")
    @XmlType(propOrder = {"reqtype", "secret", "seeContent"})
    public class TopologyRequest {
    
        private String reqtype;
        private String secret;
        private SeeContent seeContent;
    
        // 使用了JAXB注解,映射到xml中的reqtype元素
        @XmlElement(name="reqtype", required = true)
        public String getReqtype() {
            return this.reqtype;
        }
    
        // 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值
        public void setReqtype(String reqtype) {
            this.reqtype = reqtype;
        }
    
        // 使用了JAXB注解,映射到xml中的secret元素
        @XmlElement(name="secret", required = true)
        public String getSecret() {
            return this.secret;
        }
    
        // 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值
        public void setSecret(String secret) {
            this.secret = secret;
        }
    
        // 使用了JAXB注解,映射到xml中的body元素
        @XmlElement(name="body", required = true)
        public SeeContent getSeeContent() {
            return this.seeContent;
        }
    
        public void setSeeContent(SeeContent seeContent) {
            this.seeContent = seeContent;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    外部类SeeContent的定义:

    package com.thb.server.topology;
    
    import jakarta.xml.bind.annotation.XmlElement;
    import jakarta.xml.bind.annotation.XmlType;
    
    @XmlType(propOrder = {"userid", "seeid", "upseeid", "status"})
    class SeeContent{
    
        private String userid;
        private String seeid; 
        private String upseeid;
        private String status;
    
        // 使用了JAXB注解,映射到xml中body元素下面的userid元素
        @XmlElement(name="userid", required = true)
        String getUserid() {
            return this.userid;
        }
    
        // 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值
        void setUserid(String userid) {
            this.userid = userid;
        }
    
        // 使用了JAXB注解,映射到xml中body元素下面的seeid元素
        @XmlElement(name="seeid", required = true)
        String getSeeid() {
            return this.seeid;
        }
    
        // 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值
        void setSeeid(String seeid) {
            this.seeid = seeid;
        }
    
        // 使用了JAXB注解,映射到xml中body元素下面的upseeid元素
        @XmlElement(name="upseeid", required = true)
        String getUpseeid() {
            return this.upseeid;
        }
    
        // 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值
        void setUpseeid(String upseeid) {
            this.upseeid = upseeid;
        }
    
        // 使用了JAXB注解,映射到xml中body元素下面的status元素
        @XmlElement(name="status", required = true)
        String getStatus() {
            return this.status;
        }
    
        // 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值
        void setStatus(String status) {
            this.status = status;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    在cmd窗口下执行命令:

    schemagen -d D:\temp\outschema -cp D:\temp\eclipse-workspace\java_work\
    power-restful-webservice-server\src\main\java D:\temp\eclipse-workspace\java_work\power-restful-webservice-server\src\main\java\com\thb\server\topology\TopologyRequest.java
    
    • 1
    • 2

    在这里插入图片描述

    生成的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="request" type="topologyRequest"/>
    
      <xs:complexType name="topologyRequest">
        <xs:sequence>
          <xs:element name="reqtype" type="xs:string"/>
          <xs:element name="secret" type="xs:string"/>
          <xs:element name="body" type="seeContent"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:complexType name="seeContent">
        <xs:sequence>
          <xs:element name="userid" type="xs:string"/>
          <xs:element name="seeid" type="xs:string"/>
          <xs:element name="upseeid" type="xs:string"/>
          <xs:element name="status" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    首次面试凭借Java面试通关宝典,成功逆袭拿下美团offer
    Spring和Spring Boot的区别
    嵌入式通信协议----Wi-Fi协议详解(二)(基于STM32+有人物联网WIFI模块)
    LeetCode算法题解(动态规划)|LeetCoed62. 不同路径、LeetCode63. 不同路径 II
    矩阵数据_树数据结构
    10 分钟讲完 QUIC 协议。
    @JSONField注解的使用
    js ai 天花板 TensorFlow.js
    Jetson Orin使用Yolo5开源数据集训练模型检测口罩
    autohotkey 辅助工具
  • 原文地址:https://blog.csdn.net/panghuangang/article/details/134515570