2011-08-17 46 views
0

我有下面的XML文件iPhone如何解析嵌套的XML文件,并显示在tableview中

<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> 

我需要的属性类型属性值需要的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; 
} 

现在,我能够在实现代码如下得到像“Property1”和“Property2”属性值,每当我在Property1或Property2点击我得到的所有像姓名,地址,年龄,电子邮件和手机号码等元素。

但我不希望这样,如果我在Property1点击喜欢的名称,地址和年龄的各元素shuold显示,如果我在property2其元素细胞数量和电子邮件点击shuold显示

请帮我,我错了

+1

尝试使用的NSXMLParser解析和粘贴代码,如果您遇到了XMLReader类。 – 2011-08-17 12:54:14

回答

0

你必须创建一个包含名称,地址和年龄的类,并且在解析文件后,你必须在NSMutableArray中添加每个对象。 我idvise你解析你的文件使用TBXML库下载从http://www.tbxml.co.uk/TBXML/TBXML_Free.html 希望能找到这个有用