Parsing: Extracting data from pre-defined formats
Prerequisite: A good format is agreed in advance.
XML parsing
DOM:D ocument Object Model parsing
Principle: One-time read the entire XML in a stack of ways to parse each label, open the tag into the stack off tag stack when there is no element in the stack when parsing ends
Pros: Parse out all data at once and have a clear hierarchical relationship
Cons: Special memory when XML is too large
Workaround: Divide large XML into smaller XML
Sax:simple API for XML parsing based on event-driven parsing, row-by-line parsing of data (with protocol callback mechanism)
Advantage: Progressive parsing, no need to read the entire XML thus saving memory space because it is based on callback parsing, so the user can control the parsing process more flexibly
Cons: Requires users to process data themselves
Nsxmlparser is an XML parsing class for iOS that parses data using sax (infrequently used, almost unused)
Gdataxmlnode is an open source XML parsing class provided by Googol that parses the data in DOM mode (commonly parsed in this way)
usage Process:1 adding GDataxMLNode.h and GDATAXMLNODE.M files to your project
2 Enter Xcode to join the Libxml.dylib Class Library) (Project-Build phases Add on class library)
3 Find search paths segment, in header search paths add Value:/USR/INCLUDE/LIBXML2
NSString *xmlfilepath = [[nsbundlemainbundle] pathforresource:@ "Student" OfType:@ "xml"];
nsdata *data = [nsdata datawithcontentsoffile: Xmlfilepath];
gdataxmldocument *document = [[gdataxmldocument alloc] initwithdata: Data options:0 error:nil]; // parsing Complete
NSLog (@ "%@", document.) rootelement);
//XPath
/ /relative path use // = relative path
// Remove all nodes called name in the node ....
nsarray *names = [Document Nodesforxpath:@ '//student/name ' error: Nil];
nsarray *sexs = [Document Nodesforxpath:@ "//student/sex" error:nil];
nsarray *ages = [Document Nodesforxpath:@ "//student/age" error:nil];
// absolute path to give the full path / represent absolute path
nsarray *sex = [Document Nodesforxpath:@ '/students/student/sex ' error: Nil];
JSON parsing
Javascripe Object na
1 JSON format to be correct the last element is not followed by a comma ","
-(ID) objectfromjsonstring to parse the string into the specified object
-(ID) objectfromjsondata to parse data into the specified object
2
/**
* JSON parsing
* 1 Create (or import). JSON file
* 2 Get file path nsstring *filepath = [[NSBundle mainbundle] pathforresource:@ " file name " oftype:@ "JSON"]
* 3 get the data in the file nsstring NSData
* 4 Resolution a using third-party tools Jsonkit b using nsjsonserialization
*/
nsstring *filepath = [[nsbundlemainbundle] pathforresource: @"person" OfType:@ "JSON"];
// parsing
// using the third-place parsing tool Jsonkit
// by string
nsstring *jsonstr = [nsstringstringwithcontentsoffile: FilePath Encoding:nsutf8stringencoding error:nil];
nsdictionary *jsondic = [jsonstr objectfromjsonstring];
// via data
nsdata *jsondata = [nsdata datawithcontentsoffile: FilePath];
nsdictionary *jsondic2 = [Jsondata objectfromjsondata];
// using Apple's own analytic class nsjsonserialization
nsdictionary *jsondic3 = [nsjsonserializationjsonobjectwithdata: Jsondata /c8>options:(nsjsonreadingmutablecontainers | Nsjsonreadingmutableleaves) error:nil];
NSString-NSData
nsstring *thestr = @ "Hello Lanou";
nsdata *thedata = [thestr datausingencoding:nsutf8stringencoding];
NSData-NSString
nsstring *string = [[nsstringalloc] initwithdata: thedata encoding:nsutf8stringencoding];