我已经得到了 xml 解析,但是解析下面的 xml 有点困难,感谢您的建议
<?xml version="1.0" encoding="UTF-8"?>
<AllJobs>
<Mainjobs>
<job1 name="simple">
<Group name="one">
<item name>"itemone"</item name>
<item name>"itemtwo"</item name>
<item name>"itemthree"</item name>
</Group>
<Group name="two">
<item name>"itemfour"</item name>
<item name>"itemfive"</item name>
<item name>"itemsix"</item name>
</Group>
</job1>
<job2 name="medium" />
</job2>
<job3 name="hard" />
</job3>
</Mainjobs>
</AllJobs>
从上面的 xml 我试图解析下面的项目名称是“项目四,项目五,项目六”,并在表格视图中显示它们
<Group name="two">
<item>"itemfour"</item>
<item>"itemfive"</item>
<item>"itemsix"</item>
我正在这样解析,但它正在检索每个组标签中的所有项目名称
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Alljobs"]){
jobNameArray=[[NSMutableArray alloc]init];
}else if([elementName isEqualToString:@"Mainjobs"]){
// self.itemDict = [[NSMutableDictionary alloc] init];
object=[[parseObject alloc]init];
dict=[[NSMutableDictionary alloc] init];
}
else if([elementName isEqualToString:@"Group"])
{
if ([str isEqualToString:@"Two"]) {
currentElement=nil;
currentElement=[[NSMutableString alloc]init];
}
}
else if([elementName isEqualToString:@"item"])
{
str=[attributeDict objectForKey:@"name"];
NSLog(@"the attribute string is %@",[attributeDict objectForKey:@"name"]);
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(string){
[currentElement appendString:string];
NSLog(@" the element00000000%@", currentElement);
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"Mainjobs"]){
NSLog(@"the array count is %i",[jobNameArray count]);
}
else if([elementName isEqualToString:@"Job1"])
{
}
if([elementName isEqualToString:@"</item>"])
{
NSLog(@"element ---------%@",elementName);
[jobNameArray addObject:str];
}