Total Pageviews

2019/01/06

[Java] How to generate XSD file from Java Bean?

Problem
How to generate XSD file from Java Bean?

How-To
Take one Dto class for example:
package com.demo.dto;

import lombok.Data;
import lombok.ToString;

@Data
public class M7480CallDto {
    private String m7480CallId;
    private String agentId;
    private String agentName;
    private String agentExtension;
    private String callTimeStr;
    private String answerTimeStr;
    private String callDisconnectionTimeStr;
    private String endPcpTimeStr;
    private Integer callWaitingDuration;
    private Integer callAgentDuration;
    private Integer callTalkingDuration;
    private Integer callPcpDuration;
    private Integer callDisconnectionStatus;
}

Sample code for conversion:
package com.demo.util;

import java.io.File;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

import com.demo.dto.M7480CallDto;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class XsdUtils {

    private final static String XSD_DIR = "F:\\git\\demo\\src\\main\\resources\\xsd\\";

    public static void main(String[] args) throws Exception {
        XsdUtils.generateXsdFile(M7480CallDto.class, "m7480CallData.xsd");
    }

    public static void generateXsdFile(Class clazz, String fileName) throws Exception {
        try {
            (JAXBContext.newInstance(clazz)).generateSchema(new SchemaOutputResolver() {
                @Override
                public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
                    return new StreamResult(new File(XSD_DIR + fileName));
                }
            });
            log.debug("成功產生 Xsd 檔案到 " + XSD_DIR + fileName);
        } catch (IOException | JAXBException e) {
            log.error(e.getMessage());
            throw new Exception("產生 Xsd 檔案發生錯誤,錯誤原因" + e.getMessage());
        }
    }
}


Xsd file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
    <xs:complexType name="m7480CallDto">
        <xs:sequence>
            <xs:element name="agentExtension" type="xs:string" minOccurs="0"/>
            <xs:element name="agentId" type="xs:string" minOccurs="0"/>
            <xs:element name="agentName" type="xs:string" minOccurs="0"/>
            <xs:element name="answerTimeStr" type="xs:string" minOccurs="0"/>
            <xs:element name="callAgentDuration" type="xs:int" minOccurs="0"/>
            <xs:element name="callDisconnectionStatus" type="xs:int" minOccurs="0"/>
            <xs:element name="callDisconnectionTimeStr" type="xs:string" minOccurs="0"/>
            <xs:element name="callPcpDuration" type="xs:int" minOccurs="0"/>
            <xs:element name="callTalkingDuration" type="xs:int" minOccurs="0"/>
            <xs:element name="callTimeStr" type="xs:string" minOccurs="0"/>
            <xs:element name="callWaitingDuration" type="xs:int" minOccurs="0"/>
            <xs:element name="endPcpTimeStr" type="xs:string" minOccurs="0"/>
            <xs:element name="m7480CallId" type="xs:string" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
    
</xs:schema>






No comments: