0

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

2 Answers 2

1

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

There is exactly one element, called the root, or document element

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.

<allusers>
  <users>
    <username>nothan</username>
    <score>1000</score>                         
  </users>
  <users>
    <username>nothan</username>
    <score>1000</score>                         
  </users>
</allusers>
Sign up to request clarification or add additional context in comments.

Comments

0

When the parser foundCharacters method is called the int count is checked. If it is zero then the first entry in the array is replaced, and count is incremented. On the next call to this method count is not zero, so nothing is replaced, and then count is set back to zero. For the next call to this method the count is zero.....and so on.

Also bear in mind that when the data being retrieved off the net is large enough to be split into blocks by tcp/ip then the initWithContentsOfURL may not return all of it. It is better to use NSURLConnection to build up the received data as an NSData object and then pass that to the parser. In addition there may be more than one call to foundCharacters for a given element and so the element received should be built up and the placing into the array should be done at the delegate didEndElement method.

1 Comment

the count is to prevent some spaces. it has nothing to do with my problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.