【发布时间】:2011-08-17 12:37:07
【问题描述】:
我有以下 xml 文件
<Root>
<propertytype value="Property1">
<property>
<Property_Name>Name</Property_Name>
</defaultValue>
</property>
<property>
<Property_Name>Address</Property_Name>
</defaultValue>
</property>
<property>
<Property_Name>Age</Property_Name>
</defaultValue>
</property>
</propertytype>
<propertytype value="Property2">
<property>
<Property_Name>Cell Number</Property_Name>
</defaultValue>
</property>
<property>
<Property_Name>E-mail</Property_Name>
</defaultValue>
</property>
</propertytype>
</Root>
我需要的是 propertytype 属性值需要显示在 tableview 中,每当我点击该值说“Property1”时,相应的元素(名称、地址和年龄)需要显示在其他视图中
请帮帮我?
我使用了 NSXMLParser,代码粘贴在下面
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Root"]) {
//Initialize the array.
appDelegate.subtype = [[NSMutableArray alloc] init];
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"propertytype"]) {
sub=[[Subexptype alloc]init];
//Extract the attribute here.
sub.bookID=[attributeDict objectForKey:@"value"];
NSLog(@"Reading id value :%@", sub.bookID);
}
else if([elementName isEqualToString:@"property"]) {
aBook = [[Book alloc] init];
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"Root"])
return;
//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:@"propertytype"]){
[appDelegate.subtype addObject:sub];
[sub release];
sub=nil;
}
else if([elementName isEqualToString:@"property"]){
[appDelegate.books addObject:aBook];
[aBook release];
aBook = nil;
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
现在,我可以在 tableview 中获取属性值,例如“Property1”和“Property2”,每当我单击 Property1 或 Property2 时,我都会获取所有元素,例如姓名、地址、年龄、电子邮件和单元格号码。
但我不希望这样,如果我点击 Property1,则应显示其各自的元素,如姓名、地址和年龄,如果我点击 property2,则应显示其元素 Cell Number 和 E-mail
请帮我看看哪里错了
【问题讨论】:
-
尝试使用 NSXMLParser 解析,如果卡住了,粘贴代码。
-
我添加了我的代码。请帮助我
-
你能说出这里出了什么问题吗?我的意思是您遇到的错误是什么,以便缩小问题范围而不是查看代码。
-
我没有收到任何错误,而不是显示其各自的元素,而是显示所有元素
-
嗯,好的,我会根据你的代码写一个答案。
标签: iphone xml-parsing