Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Trying to return first instance of element from xml

ryantcbryantcb Posts: 328Registered Users
Im trying to get the value of an attribute from an XML the attribute appear more than once and I only want to return the result once .
here is the XML from the server and my attempted code

<T S=\"356\" T=\"0\" D=\"0\" C=\"4:00\" L=\"ABC\" DE=\"Unknown\"/>
[B]<T S=\"342\" T=\"0\" D=\"219\" C=\"6:00\" L=\"DEF\" DE=\"XYZ\"/>[/B]
<T S=\"300\" T=\"0\" D=\"410\" C=\"14:00\" L=\"EAST\" DE=\"XYZ\"/>
<T S=\"301\" T=\"0\" D=\"219\" C=\"20:00\" L=\"WEST\" DE=\"DEST\"/>
[B]<T S=\"342\" T=\"0\" D=\"219\" C=\"6:00\" L=\"DEF\" DE=\"XYZ\"/>[/B]
<T S=\"303\" T=\"0\" D=\"219\" C=\"29:00\" L=\"missing\" DE=\"look up\"/>

As you see the line in bold appears twice.
I am logging the value of attribute 'L' when the 'S' attribute is equal to string @"342".
The NSLog shows both occurrences and as I will eventually (and this is also proving more difficult that I would have thought but thats another hurdle for another time) put the result into a UILabel I only want it to return the result once and ignore if it appears again in the XML.

The code im trying is

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
BOOL=NO;
if (!BOOL) {
if ([elementname isEqualToString:@\"T\"]){
myString =[[NSString alloc]init];
if ([[attributeDict objectForKey:@\"S\"]isEqualToString:@\"342\"]) {
BOOL=YES;
}
if (BOOL) {
myString=[attributeDict objectForKey:@\"L\"];
NSLog(@\"%@\",myString);
}
}
}}


the log output is
DEF
DEF

HELP ?
Post edited by ryantcb on

Replies

Sign In or Register to comment.