views:

11034

answers:

4

Okay i have seen TouchXML, parseXML, NSXMLDocument, NSXMLParser but i am really confused with what to to do.

I have an iphone app which connects to a servers, requests data and gets XML response. Sample xml response to different set of queries is give at http://pastebin.com/f681c4b04

I have another classes which acts as Controller (As in MVC, to do the logic of fetch the data). This class gets the input from the View classes and processes it e.g. send a request to the webserver, gets xml, parses xml, populates its variables (its a singleton/shared Classes), and then responses as true or false to the caller. Caller, based on response given by the controller class, checks controller's variables and shows appropriate contents to the user.

I have the following Controller Class variables:

@interface backendController : NSObject {
NSMutableDictionary *searchResults, *plantInfoResults, *bookmarkList, *userLoginResult;
}

and functions like getBookmarkList, getPlantInfo. Right now i am printing plain XML return by the webserver by NSLog(@"Result: :%@" [NSString stringWithContentsOfURL:url])

I want a generic function which gets the XML returned from the server, parseses it, makes a NSMutableDictionary of it containing XML opening tags' text representation as Keys and XML Tag Values as Values and return that.

Only one question, how to do that?.

+2  A: 

Have you tried any of the XML Parsers you mentioned? I think this tutorial on NSXMLParser will help. This is how they set the key value of a node name:

[aBook setValue:currentElementValue forKey:elementName];

P.S. Double check your XML though, seems you are missing a root node on some of your results. Unless you left it out for simplicity.

Take a look at w3schools XML tutorial, it should point you in the right direction for XML syntax.

Sean
which root node? i dont really understand...
Shoaibi
Look at your Results of Fetch Bookmarks, your XML is <entry>...</entry> <entry>...</entry>...In XML you need a node that is the parent to all of your entries: <AllEntries><entry>...</entry><entry>...</entry>..</AllEntries>. Take a look at the w3schools tutorial, I'll add the link to the answer.
Sean
Okay i modified the XML, the new XML i the server returns is: http://pastebin.com/m6723d678 which also contains the errors/warnings when executing http://pastebin.com/m3528fa82 . I can't seem to parse XML the normal way using TouchXML. Please help.
Shoaibi
I think thats coz i am getting 4 blank lines at the start of the XML returned.
Shoaibi
Also tried with:NSString *r2 = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><AllEntries><entry><plantID>460</plantID><scientific>Alcea rosea 'Nigra'</scientific><common>Black Hollyhock</common></entry></AllEntries>";I get the following on Console:2009-04-23 08:57:06.067 Landscapedia[1926:20b] Starting XML Parsing2009-04-23 08:57:06.125 Landscapedia[1926:20b] print nodes:()No warnings but no data as well, code is same as last pastebin
Shoaibi
I haven't used TouchXML before, I can try tonight with your XML and see if I get any results. I've used NSXMLParser, the tutorial I put a link to in my answer is pretty simple to follow, I recommend trying that. I'll report back after I try it with your XML.
Sean
so any progress?
Shoaibi
A: 

Consider the following code snippet, that uses libxml2, Matt Gallagher's libxml2 wrappers and Ben Copsey's ASIHTTPRequest to parse an HTTP document.

To parse XML, use PerformXMLXPathQuery instead of the PerformHTTPXPathQuery I use in my example.

The nodes instance of type NSArray * will contain NSDictionary * objects that you can parse recursively to get the data you want.

Or, if you know the scheme of your XML document, you can write an XPath query to get you to a nodeContent or nodeAttribute value directly.

ASIHTTPRequest *request = [ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://stackoverflow.com/"];
[request start];
NSError *error = [request error];
if (!error) {
    NSData *response = [request responseData];
    NSLog(@"Root node: %@", [[self query:@"//" withResponse:response] description]);
}
else 
    @throw [NSException exceptionWithName:@"kHTTPRequestFailed" reason:@"Request failed!" userInfo:nil];
[request release];

...

- (id) query:(NSString *)xpathQuery withResponse:(NSData *)respData {
    NSArray *nodes = PerformHTMLXPathQuery(respData, xpathQuery);
    if (nodes != nil)
        return nodes;
    return nil;
}
Alex Reynolds
A: 

Check out http://www.TBXML.co.uk for a super-fast, lightweight XML parser!

Tom Bradley
+1 seems that TBXML has a much better performance than all other parsers - http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
Anurag
+1  A: 

Check Out this : How To Choose The Best XML Parser for Your iPhone Project

raaz