XML-1. Sax 2.Dom
JSON parsing provides nsjson after IOS5.0 with Sbjson and Jsonkit before 5.0
Contents of this lecture1. Basic Concepts of Analysis2.XML Data Structure3.JSON Data Structure4. Advantages and disadvantages of two types of data structures
parsing: Extracting data from pre-defined formatsthe premise of parsing: in advance to agree on a good format, data providers to provide data in a format, data acquisition by the format to obtain data
Basic XML ConceptsExtensible Markup Language
Features:data Exchange Web Service Content Management
syntax for XML data Structuresa pair of angle brackets represents a node, or a labelonly one root nodenodes can be nested with each othernodes that have no child nodes are called leaf nodes
<root> root node<room_list> child Nodes<room><room_id>100101010</room_id><room_name> Bay West Standard Room </room_name><room_price>1200</room_price></room></room_list></room>
JSON data StructureThe lightweight Data Interchange format, in a completely language-independent text format, is known as the ideal data exchange language
Data type: String numeric BOOL dictionary arrayThe dictionary separates multiple key values with ":" with " {" "}" start and end keys and values separated by ","The array is separated by "[" "]" in the middle of the data with ","
using Jsonkit for JSON parsing
#pragma mark-#pragma mark parsing json//parsing json-(void) parserjson{NSString * Path = [[NSBundle mainbundle] pathforresource:@ " Student "oftype:@" json "]; Get content from a path nsdata * data = [NSData Datawithcontentsoffile:path]; Nserror * error = NIL; Nsmutablearray * arr = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error: &error]; NSLog (@ "arr = =%@", arr); NSData * data2 = [nsjsonserialization datawithjsonobject:arr options:nsjsonwritingprettyprinted error:&error]; NSString * str = [[[NSString Alloc] initwithdata:data2 encoding:nsutf8stringencoding] autorelease]; NSLog (@ "str = =%@", str); }-(NSString *) reparserjson{nsmutabledictionary * Dic=[nsmutabledictionary dictionary]; [DiC setobject:@ "Hua" forkey:@ "1"]; Nserror * ERROR=NIL; NSData * data=[nsjsonserialization datawithjsonobject:dic options:nsjsonwritingprettyprinted error:&error]; NSLog (@ "data==%@", data); NSString * str=[[nsstringAlloc]initwithdata:data encoding:nsutf8stringencoding]; NSLog (@ "str==%@", str); return str;}
Sax parsing simple API for XML
Parsing data line by row based on event-driven parsing method (protocol callback mechanism)
Nsxmlparser
The XML parsing class that comes with iOS parses the data in sax mode
The parsing process is callback by the Nsxmlparserdelegate protocol method
Parsing process: Start Tag---value--end tag
SAX parsing is equivalent to printingDOM parsing is equivalent to copying