I have an XML file with IDs, names and URLs stored in it
I need to read the file and check the value of the ID. If it is equal to soe parameter, I will read the name and URL and stop checking. Else I advance to the next element and do the same check.
I have the following code for iPhone. It is similar to what I need to do, but I need to do it on Android:
NSString *url = @"my http url to get xml file";
NSURL *xmlURL = [NSURL URLWithString:url];
XMLParser *parser = [[XMLParser alloc] initXMLParser];
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"person"])
{
Pickers.person= person;
Pickers.fullName = fullName;
Pickers.ID = ID;
Pickers.URl = URl;
return;
}
//There is nothing to do if we encounter the Books element here.
// and release the object.
NSString *P_ID ;
NSString *P_URl;
if([elementName isEqualToString:@"ID"])
{
P_ID = currentElementValue;
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
P_ID = [[P_ID componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
P_ID = [P_ID stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
[ID addObject:P_ID];
}
else if([elementName isEqualToString:@"Name"])
{
[fullName addObject:currentElementValue];
}
else if([elementName isEqualToString:@"Symbol"])
{
[person addObject:currentElementValue];
}
}
else if([elementName isEqualToString:@"URl"])
{
P_URl = currentElementValue;
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
P_URl = [[P_URl componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
P_URl = [P_URl stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
[URl addObject:P_URl];
}
}
Also I want to know which is better to use in my case: SAX parser, DOM parser or XMLPullParser?
after a long research and trial and error i fond the most efficient and easiest (less code, less bugs) is to use Simple framework; it is amazing how easy and fun to use!
you can find it here