在Java中以编程方式生成XSD [英] Generate XSD programmatically in java

查看:1052
本文介绍了在Java中以编程方式生成XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中是否有任何API以编程方式生成XSD. 我需要从Json-Schema生成XSD,我将阅读Json Schema,并基于我在解析过程中遇到的元素,需要创建适当的XSD元素. 因此,如果有任何可以XSD元素的API,它将对我的开发过程有所帮助.

Is there any API to generate XSD programmatically in java. I need to generate XSD from Json-Schema ,I will read Json Schema and based on the elements i encounter during parsing need to create appropriate XSD elements. So if there is any API that can XSD elements it would help me in development process.

推荐答案

我使用了诸如 XSOM 的API >和将XML XML架构解析为 parse XSD,但其API不提供以编程方式生成 XSD的方法. (我想您可以尝试访问其内部实现以某种方式生成XSD,但这将由您自担风险,并且可能不明智.)

I've used API's such as XSOM and Xerces XML Schema to parse XSD's, but their API's don't offer methods to programmatically generate XSD's. (I suppose you could try to access their internal implementations to somehow generate an XSD, but that would be at your own risk and probably ill-advised.)

但是,由于XSD本身就是XML文档,因此可以通过 JAXP 以编程方式创建XSD:

However, since an XSD is an XML document itself, you could use something like DOM through JAXP to programmatically create an XSD:

package dbank.so;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.XMLConstants;


import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class SchemaGenExample {

    private final static String NS_PREFIX = "xs:";

    public static void main(String[] args) {
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

            Document doc = docBuilder.newDocument();

            Element schemaRoot = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, NS_PREFIX+"schema");
            doc.appendChild(schemaRoot);

            NameTypeElementMaker elMaker = new NameTypeElementMaker(NS_PREFIX, doc);

            Element idType = elMaker.createElement("simpleType", "idType");
            schemaRoot.appendChild(idType);
            Element idTypeRestr = elMaker.createElement("restriction");
            idTypeRestr.setAttribute("base", NS_PREFIX+"string");
            idType.appendChild(idTypeRestr);
            Element idTypeRestrPattern = elMaker.createElement("pattern");
            idTypeRestrPattern.setAttribute("value", "[0-9]{6}");
            idTypeRestr.appendChild(idTypeRestrPattern);

            Element itemType = elMaker.createElement("complexType", "itemType");
            schemaRoot.appendChild(itemType);
            Element sequence = elMaker.createElement("sequence");
            itemType.appendChild(sequence);
            Element itemIdElement = elMaker.createElement("element", "ItemId", "idType");
            sequence.appendChild(itemIdElement);
            Element itemNameElement = elMaker.createElement("element", "ItemName", NS_PREFIX+"string");
            sequence.appendChild(itemNameElement);         

            Element itemElement = elMaker.createElement("element", "Item", "itemType");
            schemaRoot.appendChild(itemElement);


            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource domSource = new DOMSource(doc);
            //to create a file use something like this:
            transformer.transform(domSource, new StreamResult(new File("mySchema.xsd")));
            //to print to console use this:
            transformer.transform(domSource, new StreamResult(System.out));
        }
        catch (FactoryConfigurationError | ParserConfigurationException | TransformerException e) {
            //handle exception
            e.printStackTrace();
        }
    }

    /*
     * Class with methods to make it more convenient to create Element nodes with
     * namespace prefixed tagnames and with "name" and "type" attributes. 
     */
    private static class NameTypeElementMaker {
        private String nsPrefix;
        private Document doc;

        public NameTypeElementMaker(String nsPrefix, Document doc) {
            this.nsPrefix = nsPrefix;
            this.doc = doc;
        }

        public Element createElement(String elementName, String nameAttrVal, String typeAttrVal) {
            Element element = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, nsPrefix+elementName);
            if(nameAttrVal != null)
                element.setAttribute("name", nameAttrVal);
            if(typeAttrVal != null)
                element.setAttribute("type", typeAttrVal);
            return element;
        }

        public Element createElement(String elementName, String nameAttrVal) {
            return createElement(elementName, nameAttrVal, null);
        }

        public Element createElement(String elementName) {
            return createElement(elementName, null, null);
        }
    }
}

将创建如下所示的mySchema.xsd:

Which would create a mySchema.xsd that looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="idType">
        <xs:restriction base="xs:string">
            <xs:pattern value="[0-9]{6}"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="itemType">
        <xs:sequence>
            <xs:element name="ItemId" type="idType"/>
            <xs:element name="ItemName" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="Item" type="itemType"/>
</xs:schema>

可用于以下XML文件:

Which could be used for an XML file such as:

<?xml version="1.0" encoding="UTF-8"?>
<Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="mySchema.xsd">
    <ItemId>123456</ItemId>
    <ItemName>Foo Bar</ItemName>
</Item>

我从来没有使用过JSON Schema,但是我想当您解析JSON或遍历JSON数据结构时,您可以整理出逻辑来执行上述示例.

I've never worked with JSON Schema, but I suppose as you parse the JSON or iterate through your JSON data structure, you could sort out the logic to do something like the above example.

这篇关于在Java中以编程方式生成XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆