Sounds like a pretty common thing to have to do, but suprisingly hard to find a good Java library to parse a WSDL file. I ended up settling on some CXF classes which seem to do the job, albeit still not very elegantly. The main reason why I'm posting here is because the use of CXFs WSDLManager and WSDLServiceBuilder does not seem particularly well documented, and thus might otherwise be lost over
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.wsdl.WSDLManager;
import org.apache.cxf.wsdl11.WSDLServiceBuilder;
import org.apache.ws.commons.schema.XmlSchema;
import org.apache.ws.commons.schema.XmlSchemaComplexType;
import org.apache.ws.commons.schema.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaSequence;
import org.apache.ws.commons.schema.XmlSchemaSequenceMember;
...
Bus bus = BusFactory.getDefaultBus();
WSDLManager wsdlManager = bus.getExtension(WSDLManager.class);
Definition definition = wsdlManager.getDefinition(wsdlUrl);
WSDLServiceBuilder wsdlServiceBuilder = new WSDLServiceBuilder(bus);
List<ServiceInfo> serviceInfos = wsdlServiceBuilder.buildServices(definition);
ServiceInfo serviceInfo = serviceInfos.get(0);
List<SchemaInfo> schemas = serviceInfo.getSchemas();
SchemaInfo schemaInfo = schemas.get(0);
XmlSchema schema = schemaInfo.getSchema();
XmlSchemaComplexType xsdType = (XmlSchemaComplexType) schema.getTypeByName("Application");
XmlSchemaSequence particle = (XmlSchemaSequence) xsdType.getParticle();
List<XmlSchemaSequenceMember> items = particle.getItems();
XmlSchemaElement type = (XmlSchemaElement) items.get(2);
String name = type.getName();
String typeName = type.getSchemaType().getName();
assertThat(typeName, is("string"));
assertThat(name, is("createdBy"));
So I'll just briefly explain that piece of atrocity...
I had an XSD complex type called "Application" stuck in the schema section of the WSDL. Obviously, I had to make some assumptions of where it lay, because of all the "get(0)" that is lying around the place. A proper implementation would search for a XSD complex type by the name, rather than using explicit indexes to pull it out.
However, whilst ugly, it did eventually give me what I needed, and most importantly it parsed out the damn WSDL into a structure in Java. There are alternatives out there, and I'm particularly interested in trying out "easywsdl" and and "membrane SOA" to see if they provide an easier to navigate tree for the schema objects. But for the time being it looks like CXF provides some not so well documented classes which do exactly what I need.
Comments ...