我是 iphone 新手。我有这个 xml 响应。如何使用 NSXML 解析器解析它?
<MyList>
<Emp>
<EmpId>918704ec-4811-45b6-a169-16bae3df3452</EmpId>
</Emp>
<Emp>
<Details>
<Name>Ravi</Name>
<EmpId>00000000-0000-0000-0000-00000000</EmpId>
</Details>
<EmpId>00000000-0000-0000-0000-00000000</EmpId>
</Emp>
</MyList>
由此我想要 EmpId(918704ec-4811-45b6-a169-16bae3df3452) 和 Name(Ravi) 的值。我不想要 EmpId(00000000-0000-0000-0000-00000000) 解析代码是:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"List"])
{
arr=[[NSMutableArray alloc] init];
drr=[[NSMutableDictionary alloc] init];
}
srr=[[NSMutableString alloc] init];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[srr appendString:string];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:@"EmpId"])
{
[drr setObject:srr forKey:@"EmpId"];
[srr release],srr=nil;
return;
}
if([elementName isEqualToString:@"Name"])
{
[drr setObject:srr forKey:@"Name"];
[srr release],srr=nil;
[arr addObject:drr];
return;
}
if([elementName isEqualToString:@"MyList"])
{
[drr release];
}
[srr release],srr=nil;
}
我需要这样的回应:
(
{
Name="Ravi";
Id="918704ec-4811-45b6-a169-16bae3df3452";
}
)