How to extract the particular string in XML without Parsing in iPhone?
I have received the data using NSURLConnection and i got the following 开发者_C百科strings, which is XML formats of string. Now i want to take the status node of string. I want to take that string without using XML parsing.
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *urlDataString = [[NSString alloc] initWithData:self.recieveData encoding:NSUTF8StringEncoding];
Return String:
<?xml version="1.0" encoding="Utf-8"?><blacksheep><status>Success</status></blacksheep>
Output:
Succeess
PLease help me out.
Thanks.
NSString* str = @"<?xml version=\"1.0\" encoding=\"Utf-8\"?><blacksheep><status>Success</status></blacksheep>";
NSString* status = [[[[str componentsSeparatedByString: @"<status>"] objectAtIndex: 1] componentsSeparatedByString: @"</status>"] objectAtIndex: 0];
But be careful: this is short but not very safe method. So you'd better add some additional checks.
Edit: better way is
NSString* status = @"Failed";
NSArray* arr = [str componentsSeparatedByString: @"<status>"];
if( [arr count] > 1 ) {
status = [[[arr objectAtIndex: 1] componentsSeparatedByString: @"</status>"] objectAtIndex: 0];
}
精彩评论