I am using a NSXMLParserDelegate method to get the data from
XML file but I cant get the data I want.
The xml file is like:
<faces>
<bounds x='70' y='250' width='438' height='438'/>
<right-eye x='208' y='424'/>
<left-eye x='368' y='426'/>
<features s-avg='0.82' s-min='0.51' s-max='0.92'>
<point id='PR' x='223' y='425' s='0.76'/>
<point id='PL' x='362' y='424' s='0.91'/>
</faces>
From this file I want to get the values for attributes x,y, and s only for the point element with attribute id equal to "PR".
Logging the value should show x=223.
This is my parsing process:
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
if ([elementName isEqualToString:@"point"])
{
NSString *idvalue = [attributeDict objectForKey:@"id"];
if (idvalue==@"PR")
{
NSString *xvalue = [attributeDict objectForKey:@"x"];
NSLog(@"%@",xvalue);
NSString *yvalue = [attributeDict objectForKey:@"y"];
NSLog(@"%@",yvalue);
NSString *svalue = [attributeDict objectForKey:@"s"];
NSLog(@"%@",svalue);
}
}
I will get a log like:
X=223 X=362 Y=425 Y=424 S=0.76 S=0.91
Values of x,y,and s for all elements.
How can I get the values only for element with the attribute ID=”PR”?
I am hoping someone can give me a reason why or if you can point me in a direction to finally get this problem solved because its becoming a real pain in the behind.
Essentially, as Matthias pointed out in comments, you want to use
NSString‘s-isEqualToStringmethod. The reason for this is thatidvalue==@"PR"is really just comparing the numeric value of the twoNSStringpointers, not their contents. The-isEqualToStringmethod actually compares the contents of the strings referenced by said pointers.Used as: