"Unexpected subelement ..." Error from the Stub Class

This section provides a tutorial example on how to troubleshoot and resolve the 'Unexpected subelement ...' error occurred in the stub class. The root cause of the error is the namespace on sub elements of the response SOAP Body does not match what defined in the WSDL document.

To understand the error, "Unexpected subelement Confirmation", from the previous tutorial example, we need to review the response first:

<soapenv:Envelope
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:hy="http://www.herongyang.com/Service/">
  <soapenv:Header/>
  <soapenv:Body>
    <hy:RegistrationResponse>
      <hy:Confirmation guest="Herong Yang" event="OpenGame"/>
      <hy:Confirmation guest="Joe Smith" event="OpenGame"/>
    </hy:RegistrationResponse>
  </soapenv:Body>
</soapenv:Envelope>

Then we need to review the code where the error was generated in stub class, RegistrationServiceStub.java:

   if (reader.isStartElement() &&
      new javax.xml.namespace.QName("","Confirmation")
         .equals(reader.getName())){
      ...                           
   }  // End of if for expected property start element
   else{
      // A start element we are not expecting indicates
      // an invalid parameter was passed
      throw new org.apache.axis2.databinding.ADBException(
        "Unexpected subelement " + reader.getLocalName());
   }

Obviously, the error is caused by namespace prefix on Confirmation elements in the response. The stub class generated from WDSL2Java is expecting "Confirmation" with no namespace.

Now, let's review the WSDL document to know why WSDL2Java decides that "Confirmation" has no namespace:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://www.herongyang.com/Service/">
      ...
      <xsd:element name="RegistrationResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="Confirmation" maxOccurs="unbounded">
              <xsd:complexType>
                <xsd:complexContent>
                  <xsd:restriction base="xsd:anyType">
                    <xsd:attribute name="guest" type="xsd:string"/>
                    <xsd:attribute name="event" type="xsd:string"/>
                  </xsd:restriction>
                </xsd:complexContent>
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      ...

As you can see, "Confirmation" is not declared as a root element in the schema, and the schema is declared with no elementFormDefault attribute specified. This means the default of elementFormDefault="unqualified" will be used and all non-root elements will be using local names only (unqualified names).

Conclusion, my Registration Web service has a code bug. The "Confirmation" element type in the response does not match the type declared in the WSDL document due to namespace issue.

To fix the my Web service code is easy. But I am not going to do it, because I want to keep it for testing purpose.

Last update: 2009.

Table of Contents

 About This Book

 Introduction to WSDL 2.0

 WSDL 2.0 Document Structure and Syntax

 WSDL Version 2.0 Part 2: Adjuncts

 WSDL 2.0 Document Examples with SOAP Binding

 WSDL 20 Programming APIs and Testing Tools

 Introduction to WSDL 1.1

 WSDL 1.1 Document Structure and Syntax

 WSDL 1.1 Binding Extension for SOAP 1.1

 soapUI 3.0.1 - Web Service Testing Tool

 WSDL 1.1 and SOAP 1.1 Examples - Document and RPC Styles

 PHP SOAP Extension in PHP 5.3.1

 Using WSDL in Perl with SOAP::Lite 0.710

 Using WSDL Document in Java with Axis2 1.4.1

Using WSDL2Java to Generate Web Service Stub Classes

 What Is WSDL2Java?

 Generating Client Side Stub Java Code

 Stub and Data Type Classes

 Using Stub and Data Type Classes

 XML Document Based Web Service Example

"Unexpected subelement ..." Error from the Stub Class

 RPC Method Based Web Service Example

 WSDL 1.1 Binding Extension for SOAP 1.2

 WSDL 1.1 and SOAP 1.2 Examples - Document and RPC Styles

 SOAP 1.2 Binding - PHP, Java and Perl Clients

 WSDL Related Terminologies

 References

 PDF Printing Version