Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate XSD programmatically in java

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.

like image 539
user2478236 Avatar asked Feb 10 '23 23:02

user2478236


1 Answers

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.)

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);
        }
    }
}

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>

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>

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.

like image 179
dbank Avatar answered Feb 13 '23 22:02

dbank