iPhone如何解析嵌套的xml文件并在tableview中显示

发布于 2024-11-29 22:28:10 字数 3117 浏览 0 评论 0原文

我有以下 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 属性值需要在表视图中显示,每当我单击该值时说“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,则应显示其各自的元素,如姓名、地址和年龄,如果我单击 property2,则应显示其元素手机号码和电子邮件,

请帮助我,我错了

I have the following xml file

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

What i need is propertytype attributes value need to display in tableview and whenever i click on that value say "Property1", the respective elements(Name,Address and Age) need to be displayed in other view

Please help me?

I used the NSXMLParser and code is pasted below

- (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;
}

Now, i can able to get attribute value like "Property1" and "Property2" in tableview and whenever i click on Property1 or Property2 i get all elements like Name,Address,Age,E-mail and Cell Number.

But i don't want this way, if i click on Property1 its respective elements like Name, Address and Age shuold be displayed and if i click on property2 its elements Cell Number and E-mail shuold be displayed

Please help me where i am wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-12-06 22:28:10

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

You have to create a class that contains Name,Address and Age and after parsing your file, you have to add each object in a NSMutableArray.
I idvise you for parsing your file to use TBXML library downloadable from http://www.tbxml.co.uk/TBXML/TBXML_Free.html
Hoping find this useful

情愿 2024-12-06 22:28:10

使用

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/

Use

XMLReader.h

This will return you a dictionary for your xml.and from there you can easily extract the required content.

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

Check the XMLReader class from here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文