2011-08-29 463 views
1

我知道這個問題已被詢問很多次。我有包含xml的響應字符串。從xml字符串獲取子字符串objective-c

<?xml version="1.0" encoding="utf-8"?> 
<response>116363111</response> 

對於每個請求我收到新的短xml字符串,我需要在「響應」節點獲得價值。我怎麼能做到這一點,而不創建XML解析器?我不想爲這個簡短的XML創建XML解析器。

回答

3

您可以使用NSScanner並從<response></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

好,謝謝,fichek) –