I am sending a query to a server and getting response using this code
NSString *urlString = @"myPHPQuery";
NSURL *parserUrl = [[[NSURL alloc] initWithString:urlString] autorelease];
NSXMLParser *parser = [[[NSXMLParser alloc] initWithContentsOfURL:parserUrl] autorelease];
[parser setDelegate:self];
[parser parse];
I can get this type of response
<users>
<username>nothan</username>
<score>1000</score>
</users>
<users>
<username>nothan</username>
<score>1000</score>
</users>
I am using this code to parse the data in Mutable Array
int arrayCount = 0;
NSString *elementname;
NSInteger * count;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
elementname = elementName;
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if([elementname isEqualToString:@"username"])
{
if ( count == 0 )
{
NSLog(@"%@" , string);
[self.playerNames replaceObjectAtIndex:arrayCount withObject:string];
arrayCount = arrayCount + 1;
count = count + 1 ;
}
else
count = 0;
}
else if([elementname isEqualToString:@"score"])
{
if ( count == 0 )
{
NSLog(@"%@" , string);
[self.scores replaceObjectAtIndex:arrayCount withObject:string];
count = count + 1 ;
}
else
count = 0;
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
for (int i = 0 ; i <10 ; i++)
{
NSLog(@"%d - %@" , i,[playerNames objectAtIndex:i]);
}
}
The problem with this code is, it retrieve only one
<users> .... </users> How can I change this code to make it retrieve more users.
Best Regards
For the sample XML you posted above, the parser ran correctly.
Extensible Markup Language (XML) 1.0 (Fifth Edition) §2.1 Well-Formed XML Documents
After the first
<users>…</users>element is parsed, the document has finished and the parser stops.To have a list if things in an XML document, you must wrap them in an outer element.