开发者

Get substring from xml string objective-c

开发者 https://www.devze.com 2023-04-01 13:29 出处:网络
I know this question has been asked many times. I\'ve response string that contains xml. 开发者_开发技巧<?xml version=\"1.0\" encoding=\"utf-8\"?>

I know this question has been asked many times. I've response string that contains xml.

开发者_开发技巧<?xml version="1.0" encoding="utf-8"?>
<response>116363111</response>

For each request i receive new short xml string, and i need to get value in "response" node. How i can do this, without creating xml parser? I don't want to create xml parser for this short xml.


You could use NSScanner and scan from <response> up to </response>.

NSString *xmlString = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<response>116363111</response>";

NSString *startTag = @"<response>";
NSString *endTag = @"</response>";
NSString *responseString;

NSScanner *scanner = [[NSScanner alloc] initWithString:xmlString];
[scanner scanUpToString:startTag intoString:nil];
scanner.scanLocation += [startTag length];
[scanner scanUpToString:endTag intoString:&responseString];
[scanner release];

NSLog(@"Response string is %@", responseString);
0

精彩评论

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