I want to create an iOS
app
which should load profiles from an XML
file on the web, check for duplicates and eliminate them. I use NSXMLParser
to parse the profiles from the XML
file but now I am confused about how to check for duplicates. The eliminated profiles should be shown in a UITableView
.
Any idea or help would be appreciated!
<profiles>
<profile>
<firstname>Amin</firstname>
<lastname>Ziarkash</lastname>
<address>Melisstokelaan</address>
<zipcode>2541GH</zipcode>
<city>DenHaag</city>
<email>[email protected]</email>
</profile>
<profile>
<firstname>Niko</firstname>
<lastname>DeVries</lastname>
<address>Sterrelaan</address>
<zipcode>3342JH</zipcode>
<city>Amsterdam</city>
<email>[email protected]</email>
</profile>
<profile>
<firstname>Marcel</firstname>
<lastname>Janssens</lastname>
<address>Voorhoeveweg</address>
<zipcode>6006SV</zipcode>
<city>Weert</city>
<email>[email protected]</email>
</profile>
<profile>
<firstname>Amin</firstname>
<lastname>Ziarkash</lastname>
<address>Melisstokelaan</address>
<zipcode>2541GH</zipcode>
<city>DenHaag</city>
<email>[email protected]</email>
</profile>
</profiles>
Try like this for parsing your XML, once you parsed then filter it accordingly:-
-(IBAction)test:(id)sender
{
self.mutableArr=[[NSMutableArray alloc]init];
NSURL *url = [[NSURL alloc]initWithString:@"file:///Users/XYZ/Desktop/test.xml"];//this is your xml file url
NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:url];
[parser setDelegate:self];
BOOL par=[parser parse];
NSLog(@"par=%d",par);
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"firstname"])
{
self.elementNm=elementName;
}
if ([elementName isEqualToString:@"lastname"])
{
self.elementNm=elementName;
}
if ([elementName isEqualToString:@"address"])
{
self.elementNm=elementName;
}
if ([elementName isEqualToString:@"zipcode"])
{
self.elementNm=elementName;
}
if ([elementName isEqualToString:@"city"])
{
self.elementNm=elementName;
}
if ([elementName isEqualToString:@"email"])
{
self.elementNm=elementName;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"profiles"])
{
NSLog(@"rootelement end");
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([string rangeOfString:@"\n"].location== NSNotFound)
{
if (string.length > 0)
{
self.mutableDict=[[NSMutableDictionary alloc]init];
[self.mutableDict setObject:string forKey:self.elementNm];
[self.mutableArr addObject:self.mutableDict];
}
}
}
//Now filtered you array after parsing xml
-(IBAction)filterArray:(id)sender
{
for (NSMutableDictionary *dict in self.mutableArr)
{
if ([dict objectForKey:@"zipcode"])
{
NSPredicate *pd=[NSPredicate predicateWithFormat:@"(zipcode == %@)", [dict objectForKey:@"zipcode"]];
[self.mutableArr filterUsingPredicate:pd];
}
}
}