1

I am calling a SOAP Web Service, the final response contains a JSON object. The web service returns data in Hex String, I use xml parser to get the right xml tag to get the JSON response. The code works fine except when the JSON contains a large amount of data. In case of large JSON the code works fine upto POINT 1, the hex string, when converted to UTF8String shows the complete xml with complete JSON embedded inside it. But after parsing, at POINT 2, I get incomplete JSON. The log output shows some of the JSON data without closing tags and majority of data. And this does not happen when the JSON response is small. JSON is I think small enough to fit in the String variable, (can someone confirm the string size limit in swift too? ) Also I should mention the same web service and parsing is working fine in android version of the application. Relevant code and sample XML is pasted below.

func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
    mutableData.appendData(data)
}


func connectionDidFinishLoading(connection: NSURLConnection!) {
    println(mutableData) // ***********POINT 1: data is in Hex string format but otherwise complete upto this point
    indicator.stopAnimating()

    var xmlParser = NSXMLParser(data: mutableData)
    xmlParser.delegate = self
    xmlParser.parse()
    xmlParser.shouldResolveExternalEntities = true



}

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
    currentElementName = elementName
}

func parser(parser: NSXMLParser, foundCharacters string: String?) {
    //************ POINT 2: This is where it goes wrong, the below log output shows an incomplete string, hence incomplete JSON and JSON parser fails
    println(string)

    if currentElementName == methodname+"Result" {

        parseMyJSON(string!)
    }
    else if currentElementName == methodname2+"Result"
    {
        parseMyJSON2(string!)
    }
}

Here is the sample response from web service.

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><CustomerInformationResponse xmlns="http://tempuri.org/"><CustomerInformationResult>this tag contains the JSON I am trying to parse</CustomerInformationResult></CustomerInformationResponse></soap:Body></soap:Envelope>
user2212736
  • 93
  • 1
  • 7
  • Please also note that the JSON object string contains about 34000 characters – user2212736 Jul 10 '15 at 08:02
  • Is there in JSON some substrings that look like XML? That could the the issue. It could "cut it" thinking that's a new tag to analyze. Try to check the length of "string" when it's cut and check it's value just after. – Larme Jul 10 '15 at 08:04
  • 2
    I have closed the question as a duplicate because I strongly assume that it is the same problem: The "foundCharacters" delegate method can be called multiple times for the same XML element. You have to concatenate the strings until the "didEndElement" method is called. – Martin R Jul 10 '15 at 08:05

0 Answers0