开发者

Problem: Parsing XML File in Objective-C/Cocoa Project for iOS

开发者 https://www.devze.com 2023-01-24 07:25 出处:网络
after searching days for a solution solving my errors, I want to share the whole problem with you. Maybe somebody have a better Idea to solve this.

after searching days for a solution solving my errors, I want to share the whole problem with you. Maybe somebody have a better Idea to solve this.

My Project uses a TabViewController to switch between Views. For every Tabview I load a separate class File to make it more clear.

All the Content I need for the views comes from XML Files. I get'em via URL. The structure looks like this:

<?xml version="1.0"?>
<rss version="2.0">
    <channel>
        <Slice id=1>
            <image>www.testurl.com/test.jpg</img>
            <title>Sample Title #1</title>
            <text>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy</text>
        </Slice>

        <Slice id=2>
            <image>www.testurl.com/test2.jpg</img>
            <title>Sample Title #2</title>
   开发者_如何学Python         <text>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy</text>
        </Slice>
    </channel>
</rss>

I get such a file for every view seperately, containing the sites content I want to show in the View.

I tried some tutorials for xml parsing and found a way parsing my files and show it up in a TableView. It works fine, but when I try to implement the files in my Project, there are several problems using the delegate.

I allready have a Delegate in my Project, managing the Navigations. Now I have a second Delegate, managing the XML Parser, but I still don't know how to make the second delegate work in my View.

Every tutorial I found uses the Projects Delegate, but what if I allready have a Delegate and just want to add the XML Parser thing in my Project.

I'm getting crazy of hacking keywords in Google, without finding anything related to my problem. This is my call for help. =D

My Question: Is there any simple way to add an dynamic xml parser to my project, that gives me an array with my contents back, as I send the URL to the Parser? I just want to define the URL in my ViewController and get the Content from the XML.

Hope u understand my Problem. I would appreciate every answer that could help me with this.


mmm.... maybe your confusion is about the delegate pattern.

A delegate is simply an object that receives messages in place of another object. Think of it as a callback mechanism.

My suggestion is that you create a separate class for parsing XML files, like this:

@interface  MyXMLParser : NSObject<NSXMLParserDelegate>

This class has a NSMutableArray of results and a parse method.

The parse method will look like this:

-(void) parse:(NSString *)xmlFilePath
{
  self.results  = [[NSMutableArray alloc] init];  

  NSURL* xmlURL = [NSURL fileURLWithPath:xmlFilePath];
  NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
  parser.delegate = self;  
  [parser parse];
  [parser release];
}

In this class you also need to implement the delegate methods. These methods will get called inside the [parser parse] call, once for each element that is found in the xml file:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *) qualifiedName attributes:(NSDictionary *)attributeDict
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 

Inside these methods you need to "understand" the elements you get and store them in the results array.

You use this parser like this

MyXMLParser* parser = [[MyXmlParser alloc] init];
[parser parse:@"myfile.xml"]
NSLog(@"%@", parser.results);
[parser release];

Hope it helps.

0

精彩评论

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