Search code examples
javaspringxsdquickbooksxjc

Formatting an XSD to produce an Array of Strings in a specific way


I do not have a lot of experience working with XSD files or SOAP, so sorry if this is a trivial request.

I am building an app to communicate with Quickbooks Desktop through the Quickbooks Web Connector and I need my web service to return an array of strings when a call is made to authenticate(strUserName, strPassword).

I am using the XJC plugin to create my Java POJO classes from the XSD file FYI.

I currently have my XSD response definition formatted like so:

  <xsd:element name="authenticateResponse">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="authenticateResult" type="xsd:string" maxOccurs="unbounded"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

And I have also tried formatting it like this:

    <xsd:element name="authenticateResponse">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="authenticateResult">
            <xsd:simpleType>
              <xsd:list itemType="xsd:string"/>
            </xsd:simpleType>
          </xsd:element>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>

My Spring server is returning calls to the method like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns2:authenticateResponse xmlns:ns2="http://developer.intuit.com/">
         <ns2:authenticateResult>291bc0f2-b22b-40b7-9326-8bb946cf91ca</ns2:authenticateResult>
         <ns2:authenticateResult/>
      </ns2:authenticateResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

But I need them to be returned like this:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://developer.intuit.com/">
    <SOAP-ENV:Body>
        <ns1:authenticateResponse>
            <ns1:authenticateResult>
                <ns1:string>15c9ce293bd3f41b761c21635b14fa06</ns1:string>
                <ns1:string></ns1:string>
            </ns1:authenticateResult>
        </ns1:authenticateResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

NOTE: The parent object should be 'ns2:authenticateResult' and its children should be 'ns2:string' as the element.

How can I make it return properly?


Solution

  • In an XSD model, every XML tag needs a corresponding XSD element declaration. Your XSD does not have any element definition for the 'string' tag. So you need to explicitly declare the 'string' element as a child of the 'authenticateResponse' element:

    <xsd:element name="authenticateResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="authenticateResult">
              <xsd:complexType>
                <xsd:sequence>
                  <xsd:element name="string" type="xsd:string" maxOccurs="unbounded">
                </xsd:sequence>
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>