aeciftci
aeciftci

Reputation: 679

Reading XML in iPhone

I have a XML File like below:

<Statistics>
     <City ID="01">
          <Town ID="04">
               <Person ID="03" Name="Jack">
               </Person>
               <Person ID="04" Name="John">
               </Person>
               <Person ID="05" Name="Peter">
               </Person>
               <Person ID="06" Name="Daniel">
               </Person>
          </Town>
          <Town>
          ...
          </Town>
     </City>
     <City>
     ...
     </City>
</Statistics>

I want to read this XML file and create an array of cities. Inside city arrays I want to have Town arrays with given IDs and inside town arrays i want to have a person list that i am going to use their names in my project. The system will be like: Return people that lives in city that has ID=".." and town that has ID="..." For example: Return people that lives in CityID="01" and TownID="04" The list will be [Jack, John, Peter, Daniel].

Is there way of doing this in iPhone SDK? If there is, can you suggest me a way of doing this or explain how to do this in a simplest way, because i am very new to this environment.

Thank you so much for the incoming answers.

Upvotes: 0

Views: 948

Answers (5)

Ahmad Kayyali
Ahmad Kayyali

Reputation: 8243

NSXMLParser is what you are looking for: NSXMLParser Class Reference

This is a Good Tutorial: Make NSXMLParser your friend


Sample Code:

 NSString* XMLString = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>...";

 NSData* data = [XMLString dataUsingEncoding:NSUTF8StringEncoding]; 

 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]

 [parser setDelegate:self]; 
 [parser setShouldProcessNamespaces:NO]; 
 [parser setShouldReportNamespacePrefixes:NO];
 [parser setShouldResolveExternalEntities:NO]; 

 [parser parse]; 

You are going to need this delegate:

 parser:didStartElement:namespaceURI:qualifiedName:attributes:

And use the attribute variable of type NSDictionary to be able to read the tag attributes:

 [[attributeDict objectForKey:@"id"] integerValue]
 [attributeDict objectForKey:@"Name"]

Don't forget to add the NSXMLParserDelegate protocol to your class:

#import <Foundation/Foundation.h>

@interface MyClass : NSObject <NSXMLParserDelegate> {
...

Upvotes: 10

dredful
dredful

Reputation: 4388

There s several XML parsing options to choose from for the iOS SDK. Here is a good read to here is a good read on those options:

http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project

Your question sounds like you would like to use XPath. If that is the case then TBXML or TouchXML may be the way to go.

Upvotes: 0

Rad&#39;Val
Rad&#39;Val

Reputation: 9251

If you can control the XML(you are the owner) why not use a plist(which uses XML btw) and then get it as arrays/dicts with [NSArray arrayWithContentsOfFile:] and then manipulate the NS structures however you want.

Upvotes: 0

Khomsan
Khomsan

Reputation: 553

Here is one example...

// RootView.h

@interface RootView : UIViewController <NSXMLParserDelegate> {
    NSXMLParser *xmlParser;
}

// AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    [rootView parseXmlFile:[[NSBundle mainBundle] pathForResource:@"Untitled" ofType:@"xml"]];
}

// RootView.m

- (void)parseXmlFile:(NSString *)pathToFile {
    BOOL success;
    NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];
    if (xmlParser) // xmlParser is an NSXMLParser instance variable
        [xmlParser release];
    xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [xmlParser setDelegate:self];
    success = [xmlParser parse]; // return value not used
    // if not successful, delegate is informed of error
}

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

    if ([elementName isEqualToString:@"City"]) {
    }
}

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

}

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

    if ([elementName isEqualToString:@"City"]) {
    }
}

Upvotes: 0

user753676
user753676

Reputation:

well the best solution is to use an xml parser of your choice

if your are new to iphone sdk and xcode etc why not working with php and simple xml or xml2array?

for what do you need that? an app?

Upvotes: 0

Related Questions