+49 228 5552576-0


info@predic8.com

Manipulating WSDL with Java API

The WSDL parser reads a WSDL document and transforms it into a Java object model. You can manipulate these objects and finally serialize them back into a WSDL document.

The code in listing 1 fetches the WSDL document from the BLZService and adds a new operation with the dedicated message elements and binding operation to it.

package sample.wsdl;

import com.predic8.wsdl.*;
import com.predic8.schema.*;
import static com.predic8.schema.Schema.*;

public class ManipulateWSDL {

  public static void main(String[] args) {
    WSDLParser parser = new WSDLParser();

    Definitions wsdl = parser.parse("http://www.thomas-bayer.com/axis2/services/BLZService?wsdl");
    
    Schema schema = wsdl.getSchema("http://thomas-bayer.com/blz/");
    schema.newElement("listBanks").newComplexType().newSequence();
    Sequence seq = schema.newElement("listBanksResponse").newComplexType().newSequence();
    seq.newElement("bank", STRING);
    seq.newElement("name", STRING);
    
    PortType pt = wsdl.getPortType("BLZServicePortType");
    Operation op = pt.newOperation("listBanks");
    op.newInput("listBanks").newMessage("listBanks").newPart("parameters", "tns:listBanks");
    op.newOutput("listBanksResponse").newMessage("listBanksResponse").newPart("parameters", "tns:listBanksResponse");
    
    Binding bnd = wsdl.getBinding("BLZServiceSOAP11Binding");
    BindingOperation bo = bnd.newBindingOperation("listBanks");
    bo.newSOAP11Operation();
    bo.newInput().newSOAP11Body();
    bo.newOutput().newSOAP11Body();
    
    System.out.println(wsdl.getAsString());
  }
}
      
Listing 1: ManipulateWSDL.java

The following listing shows the modified WSDL.

<definitions targetNamespace='http://thomas-bayer.com/blz/' xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:wsaw='http://www.w3.org/2006/05/addressing/wsdl' xmlns:tns='http://thomas-bayer.com/blz/' xmlns:http='http://schemas.xmlsoap.org/wsdl/http/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:mime='http://schemas.xmlsoap.org/wsdl/mime/' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:soap12='http://schemas.xmlsoap.org/wsdl/soap12/'>
  <documentation>BLZService</documentation>
  <types>
    <xsd:schema targetNamespace='http://thomas-bayer.com/blz/' attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:wsaw='http://www.w3.org/2006/05/addressing/wsdl' xmlns:tns='http://thomas-bayer.com/blz/' xmlns:http='http://schemas.xmlsoap.org/wsdl/http/' xmlns:mime='http://schemas.xmlsoap.org/wsdl/mime/' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:soap12='http://schemas.xmlsoap.org/wsdl/soap12/'>
      <xsd:element name='getBank' type='tns:getBankType' />
      <xsd:element name='getBankResponse' type='tns:getBankResponseType' />
      <xsd:element name='listBanks'>
        <xsd:complexType>
          <xsd:sequence />
        </xsd:complexType>
      </xsd:element>
      <xsd:element name='listBanksResponse'>
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name='bank' type='xsd:string' />
            <xsd:element name='name' type='xsd:string' />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:complexType name='getBankType'>
        <xsd:sequence>
          <xsd:element name='blz' type='xsd:string' />
        </xsd:sequence>
      </xsd:complexType>
      <xsd:complexType name='getBankResponseType'>
        <xsd:sequence>
          <xsd:element name='details' type='tns:detailsType' />
        </xsd:sequence>
      </xsd:complexType>
      <xsd:complexType name='detailsType'>
        <xsd:sequence>
          <xsd:element name='bezeichnung' type='xsd:string' />
          <xsd:element name='bic' type='xsd:string' />
          <xsd:element name='ort' type='xsd:string' />
          <xsd:element name='plz' type='xsd:string' />
        </xsd:sequence>
      </xsd:complexType>
    </xsd:schema>
  </types>
  <message name='getBank'>
    <part name='parameters' element='tns:getBank' />
  </message>
  <message name='getBankResponse'>
    <part name='parameters' element='tns:getBankResponse' />
  </message>
  <message name='listBanks'>
    <part name='parameters' element='tns:listBanks' />
  </message>
  <message name='listBanksResponse'>
    <part name='parameters' element='tns:listBanksResponse' />
  </message>
  <portType name='BLZServicePortType'>
    <operation name='getBank'>
      <input message='tns:getBank' />
      <output message='tns:getBankResponse' />
    </operation>
    <operation name='listBanks'>
      <input message='tns:listBanks' name='listBanks' />
      <output message='tns:listBanksResponse' name='listBanksResponse' />
    </operation>
  </portType>
  <binding name='BLZServiceSOAP11Binding' type='tns:BLZServicePortType'>
    <soap:binding style='document' transport='http://schemas.xmlsoap.org/soap/http' />
    <operation name='getBank'>
      <soap:operation soapAction='' style='document' />
      <input>
        <soap:body use='literal' />
      </input>
      <output>
        <soap:body use='literal' />
      </output>
    </operation>
    <operation name='listBanks'>
      <soap:operation soapAction='' style='document' />
      <input>
        <soap:body use='literal' />
      </input>
      <output>
        <soap:body use='literal' />
      </output>
    </operation>
  </binding>
  <binding name='BLZServiceSOAP12Binding' type='tns:BLZServicePortType'>
    <soap12:binding style='document' transport='http://schemas.xmlsoap.org/soap/http' />
    <operation name='getBank'>
      <soap12:operation soapAction='' style='document' />
      <input>
        <soap12:body use='literal' />
      </input>
      <output>
        <soap12:body use='literal' />
      </output>
    </operation>
  </binding>
  <binding name='BLZServiceHttpBinding' type='tns:BLZServicePortType'>
    <http:binding verb='POST' />
    <operation name='getBank'>
      <http:operation location='BLZService/getBank' />
      <input />
      <output />
    </operation>
  </binding>
  <service name='BLZService'>
    <port name='BLZServiceSOAP11port_http' binding='tns:BLZServiceSOAP11Binding'>
      <soap:address location='http://www.thomas-bayer.com:80/axis2/services/BLZService' />
    </port>
    <port name='BLZServiceSOAP12port_http' binding='tns:BLZServiceSOAP12Binding'>
      <soap12:address location='http://www.thomas-bayer.com:80/axis2/services/BLZService' />
    </port>
    <port name='BLZServiceHttpport' binding='tns:BLZServiceHttpBinding'>
      <http:address location='http://www.thomas-bayer.com:80/axis2/services/BLZService' />
    </port>
  </service>
</definitions>