membrane / soa-model

Toolkit and Java API for WSDL, WADL and XML Schema.
http://www.membrane-soa.org/soa-model/
Apache License 2.0
94 stars 73 forks source link

How to get ComplexType Elements in a list #211

Closed robnewton closed 10 years ago

robnewton commented 10 years ago

I am having trouble finding a way to get to the list of elements within a complex type's sequence. Is there a method available to get them? I cannot find it.

For instance, in the following example, I am trying to get an array of the complexType elements: celcius, and errors.

The following example complex type from within a WSDL schema I am parsing:

<xsd:complexType name="fahrenheitToCelsiusResponse">
  <xsd:sequence>
    <xsd:element name="celcius" nillable="true" type="xsd:string"/>
    <xsd:element name="errors" nillable="true" type="tns:SchemaValidationErrors"/>
  </xsd:sequence>
</xsd:complexType>

My simple parsing code:


        WSDLParser parser = new WSDLParser();

        Definitions defs = parser.parse(wsdl);

        for (Schema schema : defs.getSchemas()) {
            System.out.println("Schema");
            for (ComplexType complexType : schema.getComplexTypes()) {
                System.out.println("  " + complexType.getName());
                SchemaComponent comp = complexType.getSequence();
                System.out.println("    " + comp.toString());
            }
        }

As you can see from the output, the particles variable is put into the ToString() and contains the elements that I am trying to get.

  fahrenheitToCelsiusResponse
    sequence[name= null, particles=[element[name=celcius,type={http://www.w3.org/2001/XMLSchema}string,ref=null,embeddedType=null], element[name=errors,type={http://www.estes-express.com/tools/conversion}SchemaValidationErrors,ref=null,embeddedType=null]]]

When browsing the code for the library I can see that the particles variable is what holds the data I'm looking for. I also see a getElements() method in ModelGroup.groovy that appears to return what I'm looking for but I am not seeing it in the 1.4 distribution.

  List<Element> getElements(){
    particles.findAll{it instanceof com.predic8.schema.Element}
  }
robnewton commented 10 years ago

My mistake. I was defining the wrong return type.

Once I changed

SchemaComponent comp = complexType.getSequence();

to

Sequence sequence = complexType.getSequence();

I was able to access the elements just as I expected.

Now the sample code

        WSDLParser parser = new WSDLParser();

        Definitions defs = parser.parse(wsdl);

        for (Schema schema : defs.getSchemas()) {
            System.out.println("Schema");
            for (ComplexType complexType : schema.getComplexTypes()) {
                System.out.println("  " + complexType.getName());
                Sequence sequence = complexType.getSequence();
                for (Element e : sequence.getElements()) {
                    System.out.println("    " + e.getName() + " (" + e.getBuildInTypeName() + ")");
                }
            }
        }

Here's the output now.

  fahrenheitToCelsiusResponse
    celcius (string)
    errors (null)
predic8 commented 10 years ago

Thanks for docummenting and closing the issue!

robnewton commented 10 years ago

You're welcome and nice work on the project!

Thanks, Rob

Sent from my iPhone

On Mar 4, 2014, at 2:55 AM, Thomas Bayer notifications@github.com wrote:

Thanks for docummenting and closing the issue!

Reply to this email directly or view it on GitHubhttps://github.com/membrane/soa-model/issues/211#issuecomment-36599899 .