开发者

How to load XML file data into a UITableView?

I am retrieving an XML document from a server and would like to display it in a UITableView in my application. I have the following code so far:

@implementation textxmlViewController

@synthesize tableviews;

-(void)viewDidLoad{
    [super viewDidLoad];

    NSURL *url = [[NSURL alloc] initWithString:@"http://theserver.com/demo.xml"];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [xmlParser setDelegate:self];
    BOOL success = [xmlParser parse];

    if(success)
        NSLog(@"success");
    else
        NSLog(@"Error");
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    NSLog(string);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if (qName) {
        elementName = qName;
    }
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
        attributes:(NSDictionary *)attributeDic开发者_C百科t {
    if (qName) {
        elementName = qName;
    }
    if ([elementName isEqualToString:@"text"]) {
    }
}

I already have a UITableView in this view, my question is how can I display the XML's content in the table view?

My XML data looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Answers>
    <Answer>
        <Result>it's good</Result>
        <Evaluate>can you tell me why?</Evaluate>
    </Answer>
    <Answer>
        <Result>about 5 hours</Result>
        <Evaluate>it's too long</Evaluate>
    </Answer>
</Answers>

So how can I do it? Thanks


I would recommend to use another XML parser instead of using NSXMLParser. Ray Wenderlich wrote a good article about the different XML parsing possibilities on the iPhone(iOS): http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project

I think it'd be easier for you using a DOM based XML parser. you could use the indexPath to get the data right out of the parsed xmlData.

For example: if your using GDataXML you just need to call the method

+ (GDataXMLElement *)elementsWithName:(NSString *)name;

with name = 'Answers' which returns the Node for all the answer.

on this GDataXMLElement call the method

 - (NSArray *)elementsForName:(NSString *)name;

with name = 'Answer' which gives you all the answers as a NSArray.

You can use this array as datasource for you UITableView by returning numberOfRowsInSection with [answersArray count]. And in the cellForIndexPath method get the GDataXMLElement for the answer by calling

GDataXMLElement  * answerElement = [answersArray objectAtIndex: indexPath.row];

Just parse the answerElement for the elements 'name' and 'value' to show the data in your UITableView.


It depends on how you want the info to be displayed. At least you should implement cellForRowAtIndexPath: to return the UITableViewCell to be displayed.

According to your XML structure, you can have cells for answers, each having two UITextFields to display the result and evaluate.

Please take a look at http://adeem.me/blog/2009/05/19/iphone-programming-tutorial-part-1-uitableview-using-nsarray/ for a tutorial.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜