【问题标题】:iPhone how to parse nested xml file and display in tableviewiPhone如何解析嵌套的xml文件并在tableview中显示
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使用

    XMLReader.h
    

    这将为您的 xml 返回一个字典。您可以从那里轻松提取所需的内容。

    NSDictionary *_xmlDictionary = [[NSDictionary alloc] initWithDictionary:[XMLReader dictionaryForXMLData:responseData error:&error]];
    

    从这里检查 XMLReader 类

    http://dcraziee.wordpress.com/2013/05/29/parsing-xml-in-objective-c/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多