Extract XSD from WSDL in Java

Extract XSD from WSDL in Java

Extract XSD from WSDL in Java..

In the top-down approach of webserivce development, we create the Web service from a WSDL file. In this approach very first service definition is written up.The complete service definition, message format, transport protocol, security and everything is described in WSDL.

wsdl has Types container for data type definitions using some type system (such as XSD), xsd could be either imported using xsd:import or written inside wsdl

if the xsd is part of WSDl and sometime you need to extract the XSD from wsdl, there are no utility to do this task for you.
The wsimport tool from jdk 1.7 is used to parse an existing WSDL and generate required files (JAX-WS portable artifacts) for WSDL.

  1. Java code to extract and write xsd into separate file.
  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    
    package com.ts.common.xsd;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.io.StringWriter;
    import java.util.logging.Logger;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import org.apache.commons.io.output.WriterOutputStream;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.ls.DOMImplementationLS;
    import org.w3c.dom.ls.LSSerializer;
    public class WsdlToXsdGenerator {
    private static Logger logger =Logger.getLogger(WsdlToXsdGenerator.class.getName());
    public static void wsdlToXSD(InputStream is , OutputStream os) {
    DocumentBuilder builder;
    try {
    builder = getDocBuilder();
    Document wsdlDoc = builder.parse(is);
    Document xsdDoc = getDocBuilder().newDocument();
    populateXsdDoc(wsdlDoc, xsdDoc);
    writeToStream(xsdDoc,os);
    is.close();
    os.flush();
    os.close();
    } catch (Exception e) {
    StringWriter sw = new StringWriter();
    e.printStackTrace(new PrintWriter(sw));
    logger.error(sw.toString());
    }
    }
    private static void populateXsdDoc(Document wsdlDoc, Document xsdDoc) {
    Element root = xsdDoc.createElement("xsd:schema");
    root.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
    xsdDoc.appendChild(root);
    Element element = wsdlDoc.getDocumentElement();
    Node node = element.getElementsByTagName("wsdl:types").item(0);
    NodeList lst = node.getChildNodes();
    for (int i = 0; i < lst.getLength(); i++) {
    Node nd = lst.item(i);
    if(nd.getNodeName().equals("xsd:schema")){
    NodeList xsdNodes = nd.getChildNodes();
    for (int j = 0; j < xsdNodes.getLength(); j++) {
    Node temp = xsdNodes.item(j);
    Node toAdded = xsdDoc.importNode(temp, true);
    root.appendChild(toAdded);
    }
    }
    }
    }
    private static void writeToStream(Document document, OutputStream os) throws IOException {
    DOMImplementationLS domImplementationLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
    String xsd = lsSerializer.writeToString(document);
    PrintWriter pw = new PrintWriter(os);
    // logger.debug(xsd);
    pw.write(xsd);
    pw.flush();
    }
    private static DocumentBuilder getDocBuilder() throws ParserConfigurationException{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder;
    }
    public static void main(String[] args) {
    StringBuffer sb = new StringBuffer();
    WsdlToXsdGenerator.wsdlToXSD(new FileInputStream(new File("ts.wsdl")), new WriterOutputStream( new StringWriter(sb)));
    System.out.println(sb.toString());
    }
    }
  3. input
  4. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ts="http://techsquids.com/ts/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ts" targetNamespace="http://techsquids.com/ts/">
    <wsdl:types>
    <xsd:schema targetNamespace="http://techsquids.com/ts/" name="ts">
    <!-- Request / response types -->
    <xsd:element name="tsRequest">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="year" type="ts:year"></xsd:element>
    <xsd:element name="month" type="ts:month"></xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="tsResponse">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="year" type="ts:year"></xsd:element>
    <xsd:element name="month" type="ts:month"></xsd:element>
    <xsd:element name="status" type="ts:response"></xsd:element>
    <xsd:element name="output" type="xsd:string"></xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:simpleType name="year">
    <xsd:restriction base="xsd:int">
    <xsd:minExclusive value="1970"></xsd:minExclusive>
    <xsd:maxExclusive value="9999"></xsd:maxExclusive>
    </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="month">
    <xsd:restriction base="xsd:int">
    <xsd:minInclusive value="1"></xsd:minInclusive>
    <xsd:maxInclusive value="12"></xsd:maxInclusive>
    </xsd:restriction>
    </xsd:simpleType>
    <xsd:complexType name="response">
    <xsd:sequence>
    <xsd:element name="code" type="xsd:int"></xsd:element>
    <xsd:element name="message" type="xsd:string"></xsd:element>
    <xsd:element name="appCode">
    <xsd:annotation>
    </xsd:annotation>
    <xsd:simpleType>
    <xsd:restriction base="xsd:string">
    <xsd:enumeration value="1"></xsd:enumeration>
    <xsd:enumeration value="2"></xsd:enumeration>
    <xsd:enumeration value="3"></xsd:enumeration>
    <xsd:enumeration value="4"></xsd:enumeration>
    <xsd:enumeration value="5"></xsd:enumeration>
    <xsd:enumeration value="6"></xsd:enumeration>
    </xsd:restriction>
    </xsd:simpleType>
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:schema>
    </wsdl:types>
    <wsdl:message name="getTSDataRequest">
    <wsdl:part element="ts:tsRequest" name="parameters" />
    </wsdl:message>
    <wsdl:message name="getTSDataResponse">
    <wsdl:part element="ts:tsResponse" name="parameters" />
    </wsdl:message>
    <wsdl:portType name="TSDataService">
    <wsdl:operation name="getTSData">
    <wsdl:input message="ts:getTSDataRequest" />
    <wsdl:output message="ts:getTSDataResponse" />
    </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="TSDataServiceSOAP" type="ts:TSDataService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="getTSData">
    <soap:operation soapAction="getTSData" />
    <wsdl:input>
    <soap:body use="literal" />
    </wsdl:input>
    <wsdl:output>
    <soap:body use="literal" />
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="TSDataService">
    <wsdl:port binding="ts:TSDataServiceSOAP" name="TSDataServiceSOAP">
    <soap:address location="https://techsquids.com/" />
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>
  5. output
  6. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
     <xsd:schema targetNamespace="http://techsquids.com/ts/" name="ts">
    <!-- Request / response types -->
    <xsd:element name="tsRequest">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="year" type="ts:year"></xsd:element>
    <xsd:element name="month" type="ts:month"></xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="tsResponse">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="year" type="ts:year"></xsd:element>
    <xsd:element name="month" type="ts:month"></xsd:element>
    <xsd:element name="status" type="ts:response"></xsd:element>
    <xsd:element name="output" type="xsd:string"></xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:simpleType name="year">
    <xsd:restriction base="xsd:int">
    <xsd:minExclusive value="1970"></xsd:minExclusive>
    <xsd:maxExclusive value="9999"></xsd:maxExclusive>
    </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="month">
    <xsd:restriction base="xsd:int">
    <xsd:minInclusive value="1"></xsd:minInclusive>
    <xsd:maxInclusive value="12"></xsd:maxInclusive>
    </xsd:restriction>
    </xsd:simpleType>
    <xsd:complexType name="response">
    <xsd:sequence>
    <xsd:element name="code" type="xsd:int"></xsd:element>
    <xsd:element name="message" type="xsd:string"></xsd:element>
    <xsd:element name="appCode">
    <xsd:annotation>
    </xsd:annotation>
    <xsd:simpleType>
    <xsd:restriction base="xsd:string">
    <xsd:enumeration value="1"></xsd:enumeration>
    <xsd:enumeration value="2"></xsd:enumeration>
    <xsd:enumeration value="3"></xsd:enumeration>
    <xsd:enumeration value="4"></xsd:enumeration>
    <xsd:enumeration value="5"></xsd:enumeration>
    <xsd:enumeration value="6"></xsd:enumeration>
    </xsd:restriction>
    </xsd:simpleType>
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:schema>

    complete source code with wsdl at github inside java-common project.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.