In the actual development, JSON data parsing is more simple, generally using JSON data parsing, therefore, the program apes must be good relations with the background, let him give you json data.
XML parsing:
The iOS SDK provides Nsxmlparser and libxml2 two class libraries, plus a number of third-party library options: TBXML, Touchxml, Kissxml, TinyXML, and Gdataxml.
Parsing XML usually has two ways: Dom and sax
When the DOM parses the XML, it reads into the entire XML document and constructs a tree structure (node tree) that resides in memory, and the traversal tree structure can retrieve any XML node and read its properties and values.
When sax parses XML, it is based on the pattern of event notification, while reading the XML document side processing, do not have to wait for the entire document to be loaded before taking action, when the read parsing process encountered the need to handle the object, will be issued a notification to handle it.
Nsxmlparser:
Give me a chestnut:
<rss xmlns:content= "http://purl.org/rss/1.0/modules/content/" xmlns:dc= "HTTP://PURL.ORG/DC/ELEMENTS/1.1/" version= "2.0" > Head tags, the inside of the xmlns, can be regarded as a property
<channel>
<title> hehe </title>
-(BOOL) parser: (nsstring*) string{//System comes with nsxmlparser *par = [[[Nsxmlparser alloc] initwithdata:[string datausing Encoding:nsutf8stringencoding]]autorelease]; [Par setdelegate:self];//set Nsxmlparser object resolution method proxy return [par parse];//invoke proxy parsing Nsxmlparser object to see if parsing succeeded}} #pragma mark Xmlparser//step 1: Ready to Parse-(void) Parserdidstartdocument: (Nsxmlparser *) parser{//NSLog (@ "%@", Nsstringfromselector (_ CMD)); }//step 2: Prepare resolution node-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString * ) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict{//NSLog (@ "%@", Nsstringfromselector (_cmd));} Step 3: Get the content between the end-to-end nodes-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string{NSLog (@ "%@", Nsstringfromselector (_cmd));} Step 4: Resolve the current node-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString *) qname{NSLog (@ "%@", NsstringfromseLector (_cmd));} Step 5; Parse end-(void) Parserdidenddocument: (Nsxmlparser *) parser{//NSLog (@ "%@", Nsstringfromselector (_cmd));} Get CDATA block data-(void) Parser: (Nsxmlparser *) parser Foundcdata: (NSData *) cdatablock{//NSLog (@ "%@", Nsstringfromselector (_cmd));}
Here is an explanation of Gdataxml:
Gdaraxml: This is a set of Google developed Dom way to parse the XML class library, support to read and modify the XML document, support XPath way query.
Installation Instructions:
1. Download "Gdata-objective-c client library" from Http://code.google.com/p/gdata-objectivec-client/downloads/list.
2. Unzip the file, locate the Source\xmlsupport, and drag the GDataXMLNode.h and GDATAXMLNODE.M files into the project
3. Select the project and select the "Build Settings" tab
4. Switch the "Basic" tab at the top of the build Settings page to "all"
5. Find the "Paths\header Search Paths" entry and add "/USR/INCLUDE/LIBXML2" to the list
6. Find the "Linking\other Linker Flags" entry and add "-LXML2" to the list,
In the header of the code file that needs to call Gdataxml, add:
#import "GDataXMLNode.h"
Examples of parsing DOM document objects:
NSString *filepath = [[NSBundle mainbundle] pathforresource:@ "test" oftype:@ "xml"];
NSData *xmldata = [[Nsmutabledata alloc] initwithcontentsoffile:filepath];
Nserror *error;
Gdataxmldocument *doc = [[Gdataxmldocument alloc] Initwithdata:xmldata options:0 error:&error];
if (doc = = nil) {return nil;}
NSLog (@ "%@", doc.rootelement);
[Doc release];
[XmlData release];
JSON parsing:
NSString * Urlstr = [NSString stringwithformat:@ "http://121.40.54.251:80/handheldkitchen/api/more/hotwater! Gethotwaterlist.do?id=%lu&page=1&pagerecord=12&is_traditional=0 ", (long) _iid]; Nsurl * url = [Nsurl urlwithstring:urlstr]; Nsmutableurlrequest * request = [nsmutableurlrequest Requestwithurl:url]; Request. HttpMethod = @ "GET"; [Self.detailssourcearray removeallobjects]; [Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {if (data) {Nsmutabledictionary * d IC = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil]; Nsmutablearray * array = [dic valueforkey:@ "Data"]; For (Nsmutabledictionary * dic in array) {_right = [[Rightmodel alloc]init]; [_right setvaluesforkeyswithdictionary:dIC]; [Self.detailssourcearray Addobject:_right]; [_right release]; } }];
IOS GET, Post data parsing