java生成XSD(xml)文档

java生成XSD(xml)文档

工具类

public class xsdUtils{


    /**
     * 生成XSD并下载
     * @param root  表头名称
     * @param archivesList 插入数据
     * @param path 下载路径
     */
    public static void xsdAdd(String root, ContractArchivesVo archivesList, String path){
        try {
            // 初始化一个XML解析工厂
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            // 创建一个DocumentBuilder实例
            DocumentBuilder builder = factory.newDocumentBuilder();
            // 创建一个Document实例
            Document doc = builder.newDocument();
            // standalone用来标识改文件是否呼叫其他外部的文件,"yes"表示没有呼叫外部文件
            doc.setXmlStandalone(true);

            String schema = "http://www.w3.org/2001/XMLSchema";

            Element schemaElement = doc.createElement("xs:schema");
            schemaElement.setAttribute("name","mySchema");
            schemaElement.setAttribute("xmlns:xs",schema);

            Element rootElement = doc.createElement("xs:element");
            rootElement.setAttribute("name",root);
            schemaElement.appendChild(rootElement);

            Element complexType = doc.createElement("xs:complexType");
            rootElement.appendChild(complexType);

            Element sequence = doc.createElement("xs:sequence");
            complexType.appendChild(sequence);
            // 拿到属性值
            Field[] field = ContractArchivesVo.class.getDeclaredFields();
            for (int i = 0; i < field.length; i++) {
                if(field[i].isAnnotationPresent(ChkParam.class)) {
                    // 注释拿到属性中文解释
                    ChkParam annotation = field[i].getAnnotation(ChkParam.class);
                    // 反射拿到属性类型
                    Class<?> type = field[i].getType();
                    char[] cs=type.getName().toCharArray();
                    cs[0]-=32;
                    // 尾结点新增
                    Element element = doc.createElement("xs:element");
                    element.setAttribute("name",annotation.name());
                    element.setAttribute("type","xs:string");
                    Object getMethod = getGetMethod(archivesList, field[i].getName());
                    String textContent = ObjectUtils.toString(getMethod, "");
                    element.setTextContent(textContent);
                    sequence.appendChild(element);
                }
            }

            doc.appendChild(schemaElement);
            //这里是固定格式
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();//作用,将xml文档转换为其他格式
            transformer.setOutputProperty("encoding", "UTF-8");
            transformer.transform(new DOMSource(doc), new StreamResult(new File(path)));

        }catch (Exception e){
            log.error("转换失败:"+e.getMessage());
        }
    }

    
    /**
     * 根据属性,获取get方法
     * @param ob 对象
     * @param name 属性名
     * @return
     * @throws Exception
     */
    public static Object getGetMethod(Object ob , String name)throws Exception{
        Method[] m = ob.getClass().getMethods();
        for(int i = 0;i < m.length;i++){
            if(("get"+name).toLowerCase().equals(m[i].getName().toLowerCase())){
                    return m[i].invoke(ob);
            }
        }
        return null;
    }


}

实体类

@Data
public class ContractArchivesVo {

    /** id */
    private Long id;

    /** 名称*/
    @ChkParam(name = "名称")
    private String name;

    /** 性别 */
    @ChkParam(name = "性别")
    private String sex;

    /** 邮箱 */
    private String email;

}

ChkParam

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ChkParam {
    /**
     * 检查参数属性名称
     * @return
     */
    public String name() default "";

    /**
     * 参数类型
     * @return
     */
    public String type() default "";

    /**
     * 备用字段
     * @return
     */
    public String dac() default "";
}

运行

    public static void main(String[] args) {
        ContractArchivesVo archivesVo = new ContractArchivesVo();
        archivesVo.setName("法外狂徒");
        xsdAdd.xsdAdd("测试",archivesVo,"C:\Users\data\text.xml")
    }

返回

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" name="mySchema">
  <xs:element name="测试">
    <xs:complexType>
	  <xs:sequence>
	    <xs:element name="名称" type="xs:string">法外狂徒</xs:element>
	    <xs:element name="性别" type="xs:string"/>
      </xs:sequence>
	</xs:complexType>
  </xs:element>
 </xs:schema>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值