我將查詢發送到服務器,並得到響應使用此代碼解析XML數據轉換成NSMutable數組列表
NSString *urlString = @"myPHPQuery";
NSURL *parserUrl = [[[NSURL alloc] initWithString:urlString] autorelease];
NSXMLParser *parser = [[[NSXMLParser alloc] initWithContentsOfURL:parserUrl] autorelease];
[parser setDelegate:self];
[parser parse];
我能得到這個類型的響應
<users>
<username>nothan</username>
<score>1000</score>
</users>
<users>
<username>nothan</username>
<score>1000</score>
</users>
我使用這個代碼解析在可變數組數據
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]);
}
}
這段代碼的問題是,它僅檢索一個
<users> .... </users>
如何更改此代碼以使其檢索更多用戶。
問候
讓我試試這:) – user996428