Vishnu
Vishnu

Reputation: 2243

Parse xml value from xml file in server of this sample code

I've created an app using xml parser from a tutorial.But this tutorial found to be easy.In this an xml file is parsed from local xml..But if i need to parse an value from xml file say www.xxxxx.com/xxx.xml..how can i modify the code...Guidance please..

- (void) applicationDidFinishLaunching:(UIApplication *)application {

// find "sample.xml" in our bundle resources
NSString *sampleXML = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"xml"];
NSData *data = [NSData dataWithContentsOfFile:sampleXML];

// create a new SMXMLDocument with the contents of sample.xml
NSError *error;
SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];

// check for errors
if (error) {
    NSLog(@"Error while parsing the document: %@", error);
    return;
}

// demonstrate -description of document/element classes
NSLog(@"Document:\n %@", document);

// Pull out the <books> node
SMXMLElement *books = [document.root childNamed:@"books"];

// Look through <books> children of type <book>
for (SMXMLElement *book in [books childrenNamed:@"book"]) {

    // demonstrate common cases of extracting XML data
    NSString *isbn = [book attributeNamed:@"isbn"]; // XML attribute
    NSString *title = [book valueWithPath:@"title"]; // child node value
    float price = [[book valueWithPath:@"price"] floatValue]; // child node value (converted)

    // show off some KVC magic
    NSArray *authors = [[book childNamed:@"authors"].children valueForKey:@"value"];

    NSLog(@"Found a book!\n ISBN: %@ \n Title: %@ \n Price: %f \n Authors: %@", isbn, title, price, authors);
}
 }

I've tried many tutorials for so many hours and finally i got well from using these code in my app.But i need to import xml file from server...

Upvotes: 0

Views: 1164

Answers (2)

George
George

Reputation: 4029

Well , it cuts down to 2 things: getting the XML string and parsing it. In order to get the xml , you will have to simply write something like this:

NSError *error = nil; NSString *xmlString = [NSString stringWithContentsOfURL:[NSURL urlWithString:@"www.xxxxx.com/xxx.xml"] encoding:NSUTF8StringEncoding error:&error]

After that you feed this string to a parser. As far as I know , TBXML is the fastest , and it's easy to use. You can get it from here.

After that you need to traverse the nodes of the XML which is easy and you can find a lot of examples. Here's some documentation.

Cheers!

Upvotes: 1

Rui Peres
Rui Peres

Reputation: 25917

Obviously I am not going to write all the code you would need (Server Access + XML Parser + Protocols, to at least have a decent architecture), this is not a blog. So:

  1. Start by downloading the XML file from the server. You can use NSURLConnection or you can use a 3rd party library (MKNetworKit for example).
  2. After you get the data (NSData) from the server, you can fill your parser with it.

In a nutshell that's it. Just a pointer:

  1. Please, just don't put your XML Parser inside applicationDidFinishLaunching:, it doesn't make any sense. If you need to use it when the the applicationDidFinishLaunching:, it's perfectly valid, create an appropriate class for the XML Parser.

Upvotes: 3

Related Questions