-2

Possible Duplicate:
Xml parsing in iOS tutorial

I have XML file in my Xcode project and I want to parse this XML file and show data in UITableView. Since I am new to xml parsing it's getting difficult for me to do the same. How to parse the below xml?

My xml file is:

<application>
    <app_data>
        <wayHome>
            <data>
                <title>159 Darby St Bus Stop</title>
                <category>BUS</category>
                <lat>-32.9320366</lat>
                <long>151.7712731</long>
                <image>159 Darby St Bus Stop.png</image>
            </data>
            <data>
                <title>Beaumont St and Denison St Taxi Rank</title>
                <category>TRAIN</category>
                <lat>-32.9320366</lat>
                <long>151.7712731</long>
                <image>Beaumont St and Denison St Taxi Rank.png</image>
            </data>
            <data>
                <title>Beaumont St Bus Shop next to ANZ</title>
                <category>BUS</category>
                <lat>-32.9227304</lat>
                <long>151.7472369</long>
                <image>Beaumont St Bus Shop next to ANZ.png</image>
            </data>
         </wayHome>
     </app_data>
</application>
Cœur
  • 37,241
  • 25
  • 195
  • 267
vijay
  • 1,235
  • 1
  • 11
  • 32

1 Answers1

3

Use NSXMLParserDelegate Methods to parse the File.

In yourInterface.h file include NSXMLParserDelegate 

@implementation yourInterface.m file: use below code

NSURL* xmlFile = [NSURL  fileURLWithPath:[[NSBundle mainBundle]
                                           pathForResource:@"yourXMLfile"
                                           ofType:@"xml"]];

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlFile];

//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];

//Set delegate
[xmlParser setDelegate:parser];

//Start parsing the XML file.
BOOL success = [xmlParser parse];

if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");

Use Delegate Methods to parse The File. I assume that you need to parse the "Data" tags. Here, you can check that ElementName is "Data" OR NOT. And similar for the innertags of "Data". Here you can maintain Array OR Dictionary For Data Elements.

so your work is to do in Delegate methods:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;

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

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
krunal
  • 452
  • 3
  • 11