http://www.cbeninati.com/i/?p=131
In order to parse XML into an NSDictionary to be used in my iOS project, I have found and implementedXMLReader created by Troy Brant.
The implementation is simple to use as you can see for yourself:
1
2
3
4
5
6
7
8
| NSString *testXMLString = @"<items><item id=\"0001\" type=\"donut\"><name>Cake</name><ppu>0.55</ppu><batters><batter id=\"1001\">Regular</batter><batter id=\"1002\">Chocolate</batter><batter id=\"1003\">Blueberry</batter></batters><topping id=\"5001\">None</topping><topping id=\"5002\">Glazed</topping><topping id=\"5005\">Sugar</topping></item></items>";// Parse the XML into a dictionaryNSError *parseError = nil;NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];// Print the dictionaryNSLog(@"%@", xmlDictionary); |
For my project however, I needed to parse through some XML returned from a webservice, so I usedASIHTTPRequest, since I was already implementing it for my project.
Ultimately, my code ended up looking like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[request startSynchronous];NSError *error = [request error];if (!error) {NSString *response = [request responseString];// NSLog(@"%@", response);NSError *parseError = nil;NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:response error:&parseError];// Print the dictionaryNSLog(@"%@", xmlDictionary);} |
No comments:
Post a Comment