我想创建一个iOS
app
,它应该从网络上的XML
文件加载配置文件,检查重复项并消除它们。我使用NSXMLParser
来解析XML
文件中的配置文件,但现在我对如何检查重复项感到困惑。消除的配置文件应显示在UITableView
。
任何想法或帮助都将不胜感激!
<profiles>
<profile>
<firstname>Amin</firstname>
<lastname>Ziarkash</lastname>
<address>Melisstokelaan</address>
<zipcode>2541GH</zipcode>
<city>DenHaag</city>
<email>a.ziarkash@hotmail.com</email>
</profile>
<profile>
<firstname>Niko</firstname>
<lastname>DeVries</lastname>
<address>Sterrelaan</address>
<zipcode>3342JH</zipcode>
<city>Amsterdam</city>
<email>niko@hotmail.com</email>
</profile>
<profile>
<firstname>Marcel</firstname>
<lastname>Janssens</lastname>
<address>Voorhoeveweg</address>
<zipcode>6006SV</zipcode>
<city>Weert</city>
<email>marcel.j@hotmail.com</email>
</profile>
<profile>
<firstname>Amin</firstname>
<lastname>Ziarkash</lastname>
<address>Melisstokelaan</address>
<zipcode>2541GH</zipcode>
<city>DenHaag</city>
<email>a.ziarkash@hotmail.com</email>
</profile>
</profiles>
答案 0 :(得分:1)
尝试这样解析XML,解析后再对其进行过滤: -
-(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];
}
}
}
答案 1 :(得分:0)
你可以使用这个漂亮的类扩展 https://github.com/nicklockwood/XMLDictionary
NSDictionary *dict = [NSDictionary dictionaryWithXMLData:data];
然后,您可以将这些词典映射到您自己的自定义类