开发者

NSXMLParser to parse SOAP response

so, I have received a soap response from a service and now I need to parse it. I am getting stuck. For this response, I want to grab the "Gzk0P" value toward the bottom. See my parse method below the xml. thanks!

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://mysvc.myservice.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 开发者_C百科xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:createSubmissionResponse>
<return xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">error</key>
<value xsi:type="xsd:string">OK</value>
</item>
<item>
<key xsi:type="xsd:string">link</key>
<value xsi:type="xsd:string">Gzk0P</value>
</item>
</return>
</ns1:createSubmissionResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    if ([elementName isEqualToString:@"key"]) {

        NSString *title = [attributeDict valueForKey:@"value"]; 
        NSString *test = @"";
    }

}


You are doing wrong you cant access the values form attributed dict. attributed dict is used when values written in the same tag as attribute see this

lt key gt xsi:type="xsd:string" value="Gzk0P">link lt /key gt

But here is condition is different, value is an separate tag in tag thats why you need to reach inside the value tag and in foundCharacters method you can access that value.

code some thing like this

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {


        currentElementValue=string;

    NSLog(@"Processing Value: %@", currentElementValue);

}


Try this way, though I have not tested this one,but I hope this will work

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

    key = nil;
    [stringValue release];
    stringValue = nil;
    if([elementName isEqualToString:@"item"]){
    if([elementName isEqualToString:@"value"]){
    key = @"value";
        return;
    }}


There are two things associated with your xml.

  1. Will the xml always be the same as you posted and you are interested in the value from the last tag?
  2. If its not going to be same then what is the value you will be interested in?

For 1 - You can simply parse and use the last found character for the value tag as your output.

For 2 - You will have to find a way where you can identify which value you would want to parse. In this case the xml will need to be edited , either modify your tag or add a attribute.

Drop a comment in case you need more info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜