3331

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.

Answer1:

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.

Recommend

  • How do I encode arbitrary data to XML using Java 1.4 and SAX?
  • Transformation of multiple input files
  • How to add an element in this XML file using DOM parser?
  • Reading xsl from jsp
  • Can you 'click' loaded content in jQuery?
  • Why is this button causing my layout to break?
  • What is the best way to include a style sheet for a specific page?
  • How do I include screenshots of the full page in my serenity report (and not only of the viewport) u
  • ng-repeat not working with table but works with list
  • getelement by class name for clicking
  • when does setTimeout start executing in a inline
  • pythonanywhere - How do I use websockets to transmit messages as per the web2py messaging example?
  • how to ignore xml namespaces?
  • Is looping through all style sheets and classes a good idea in JavaScript?
  • Unzip archive in .Net CORE 1.0
  • Making Google Visualization - Annotation Chart to work in GWT
  • Streaming screenshots over WebRTC as a video stream from iOS
  • Cannot get text from text area
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • How can I restyle a word when rendering a pdf with pdf.js?
  • Uncaught TypeError: $(…).select2 is not a function
  • Remove changes from one element when event occurs on another element?
  • Button click event not firing in jQuery
  • Chart.js Multiple dataset
  • How do I get HTML corresponding to current DOM tree?
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • ImageMagick, replace semi-transparent white with opaque white
  • Cannot connect to cassandra from Spark
  • Cross-Platform Protobuf Serialization
  • Change an a tag attribute in JavaScript based on screen width
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?