I am currently using NSURLConnection to download some date from a database through a php script on a server.
The connection works fine and data is received correctly. However I have a problem when I come to parse the data.
I am currently trying to use the NSXMLParser to parse the data however this is failing with Error code 4, I think this is because what I retrieve is not entirely XML. If it is entirely XML it works.
Here is an example of the data retrieved :
43534545-45345345-34534554|iPhone emulator|<provdoc>
<characteristic type="P1">
...
</characteristic>
</provdoc>
And what I'd like to do is split the data into:
43534545-45345345-34534554
and:
iPhone emulator
and
<provdoc>
<characteristic type="Profile1">
...
</characteristic>
</provdoc>
So Im guessing that I should do that in the following function where I take in the data, I need to know how I split the data into the above three sections?
So I end up with two strings, the first one with the numbers and the second with the iPhone emulator bit and then the data that can be send through the NSXMLParser.
Can anyone point me in开发者_JAVA百科 the right direction as yo what I need yo accomplish that?
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.responseData appendData:data];
}
Convert the final NSData to an NSString:
NSString *someString = [[NSString alloc] initWithData:hashedData encoding:NSUTF8Encoding];
Then you'll need to start splitting up that string. Here's an example of getting the first portion:
NSRange end = [someString rangeOfString:@"<"];
NSString *str = [someString substringWithRange:NSMakeRange(0, end.location)];
Then split up the first one:
NSArray *initialItems = [str componentsSeparatedByString:@"|"];
You should be able to figure out the rest with the above techniques.
精彩评论