0

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...

2 Answers 2

3

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.
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not asking the code and i know that SO is not for that...I just asked What is the architecture or basic groundwork that i have to do for my need...and tks for ur answer...
"how can i modify the code...Guidance please.." - Guidance have been given. If you need further help, let me know Dany. Sorry if I was harsh, stackoverflow made me that way.
1

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!

1 Comment

TBXML link which u have attached only deals with static xml file..Can u tell me about dynamic file...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.