views:

56

answers:

2

Using grails 1.2.2 with groovy 1.6.8 .. Reading a a web service and trying to process response ..

Response is shown below and validates as correct xml .. (sorry for length) ..

<soap:Body>
  <AddProductEventResponse xmlns="http://tempuri.org/"&gt;
     <AddProductEventResult>
        <xs:schema id="AddProductEventResult" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
           <xs:element name="AddProductEventResult" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
              <xs:complexType>
                 <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="AddProductEventResult">
                       <xs:complexType>
                          <xs:sequence>
                             <xs:element name="ErrorCode" type="xs:string" minOccurs="0"/>
                             <xs:element name="ErrorNumber" type="xs:int" minOccurs="0"/>
                             <xs:element name="ErrorDesc" type="xs:string" minOccurs="0"/>
                          </xs:sequence>
                       </xs:complexType>
                    </xs:element>
                 </xs:choice>
              </xs:complexType>
           </xs:element>
        </xs:schema>
        <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
           <AddProductEventResult xmlns="">
              <AddProductEventResult diffgr:id="AddProductEventResult1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
                 <ErrorCode>S</ErrorCode>
                 <ErrorNumber>0</ErrorNumber>
                 <ErrorDesc>Success</ErrorDesc>
              </AddProductEventResult>
           </AddProductEventResult>
        </diffgr:diffgram>
     </AddProductEventResult>
  </AddProductEventResponse>
</soap:Body>
</soap:Envelope>

I try to parse this using

def myXml = new XmlSlurper().parseText(result)

where result is the above message and i get an error ...

2010-06-19 06:08:03,665 [http-8080-2] ERROR errors.GrailsExceptionResolver - Premature end of file. org.xml.sax.SAXParseException: Premature end of file. at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)

which is the above xmlSlurper statement ..

If i copy and run from groovyConsole i don't have the problem .. I created a stripped down Grails project and ran it from there without problems either .. I'm getting a little bit desperate to get this sorted (have posted on Grails site to) so has anyone any idea ?

A: 

The above shown XML document cannot be validated. There is an ending tag </soap:Envelope> but no according start tag. Are you sure you are providing the complete XML here?

This works without any parsing exception:

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"&gt;
 <soap:Body>
  <AddProductEventResponse xmlns="http://tempuri.org/"&gt;
     <AddProductEventResult>
        <xs:schema id="AddProductEventResult" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
           <xs:element name="AddProductEventResult" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
              <xs:complexType>
                 <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="AddProductEventResult">
                       <xs:complexType>
                          <xs:sequence>
                             <xs:element name="ErrorCode" type="xs:string" minOccurs="0"/>
                             <xs:element name="ErrorNumber" type="xs:int" minOccurs="0"/>
                             <xs:element name="ErrorDesc" type="xs:string" minOccurs="0"/>
                          </xs:sequence>
                       </xs:complexType>
                    </xs:element>
                 </xs:choice>
              </xs:complexType>
           </xs:element>
        </xs:schema>
        <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
           <AddProductEventResult xmlns="">
              <AddProductEventResult diffgr:id="AddProductEventResult1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
                 <ErrorCode>S</ErrorCode>
                 <ErrorNumber>0</ErrorNumber>
                 <ErrorDesc>Success</ErrorDesc>
              </AddProductEventResult>
           </AddProductEventResult>
        </diffgr:diffgram>
     </AddProductEventResult>
  </AddProductEventResponse>
 </soap:Body>
</soap:Envelope>
codescape
A: 

Forgive me , i missed first line off by accident .. should have had

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" mlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;

as first line ..

Problem still remains BUT i've traced the error to something which looks really inoccuous ..

In a controller i have ..

def result = myService.productListService()

  if (result != "Error") {

  def xml = new XmlSlurper().parseText(result)   

which throws my original error. In my service i have ..

def productListService() {

....

def  someList  = processRequest(conn, msgBody, "Products")

return someList
....

}

and a helper method ..

// Helper routines  ..

String processRequest(conn, dataString, serviceName) {

conn.setRequestMethod("POST")
conn.doOutput = true

Writer writer = new OutputStreamWriter(conn.outputStream)
writer.write(dataString)
writer.flush()
writer.close()
conn.connect()

if (conn.responseCode == 200 || conn.responseCode == 201){
  println "Response .. "
  println conn.content.text      <---------  Remove this and problem goes away !!!
  return conn.content.text
}

println serviceName + " FAILED .. "
println  conn.responseCode
println  conn.responseMessage

return "Error"

}

The xml is the conn.content.text variable and my helper routine echoes it to the screen for testing. It also has the unfortunate side effect of being the cause of my problem ! Without the outputing of the line everything works fine , with it i get the aforementioned xmlSlurper error .. very strange (to me at least) .. I don't understand that ..

Rob Morning