In a sense, most IOS applications need to communicate with remote Web servers in some way. I will briefly introduce the following XML and JSON parsing for your reference only. please correct me if any errors exist. Thank you.
XML Parsing
1. The SAX Parser is a streaming parser that traverses the entire XML document one by one and returns parsed data by returning the function. most of the sax parsers accept a URL parameter, and the target data will be returned after parsing. for example, the nsxmlparser class has a method named initwithcontentsofurl:
(ID) initwithcontentsofurl :( nsurl *) URL;
You only need to use the URL to initialize a parser. nsxmlparser will handle the remaining tasks and return parsed data by calling the delegate method defined in nsxmlparserdelegate. Common methods include:
# Pragma mark-nsxmlparserdelegate
Method
-(Void) parser :( nsxmlparser *) parser didstartelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri qualifiedname :( nsstring
*) QNAME attributes :( nsdictionary *) attributedict
{
}
-(Void) parser :( nsxmlparser *) parser foundcharacters :( nsstring *) String
{
}
-(Void) parser :( nsxmlparser *) parser didendelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri qualifiedname :( nsstring
*) QNAME
{
}
Since the parser uses a delegate to return data, each object to be processed must have a nsobject subclass that implements nsxmlparserdelegate. This makes the code less concise than Dom. so what is the DOM parser? Please refer to the 2 Dom parser. The Dom parser, as its name suggests, is also a parser. Unlike the SAX Parser, the DOM parser loads the entire XML file into the memory before parsing. the advantage of the DOM parser is that you can use XPath to query and access random data, and do not need to use the same delegate as the sax model. nsxmldocument in Mac OS X SDK is an objective-C-based Dom parser, while IOS does not have a built-in Dom Parser for yuoc. however, you can use libxml2 or a third-party OC package, such as libxml2-based kissxml, touchxml, and gdataxml. here we will mainly introduce Google's gdataxml
-
(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
{
Self. Window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease];
Self. Window. backgroundcolor = [uicolor whitecolor];
[Self. Window makekeyandvisible];
/************** Gdatamxl resolution *****************/
// Obtain the XML file of the project directory
Nsstring * filepath = [[nsbundle mainbundle] pathforresource: @ "users" oftype: @ "XML"];
Nsdata * xmldata = [[nsdata alloc] initwithcontentsoffile: filepath];
// Use nsdata to initialize the object
Gdataxmldocument * Doc = [[gdataxmldocument alloc] initwithdata: xmldata options: 0 error: Nil];
// Obtain the root node
Gdataxmlelement * rootelement = [Doc rootelement];
// Obtain the nodes under the root node
Nsarray * Users = [rootelement elementsforname: @ "user"];
For (gdataxmlelement * user in users ){
Nslog (@"************************************ *********************************");
// ID attribute of the user Node
Nsstring * userid = [[user attributeforname: @ "ID"] stringvalue];
Nslog (@ "User ID is: % @", userid );
// Obtain the value of the Name Node
Gdataxmlelement * nameelement = [[user elementsforname: @ "name"] objectatindex: 0];
Nslog (@ "name is: % @", [nameelement stringvalue]);
// Obtain the value of the age Node
Gdataxmlelement * ageelement = [[user elementsforname: @ "Age"] objectatindex: 0];
Nslog (@ "Age is: % @", [ageelement stringvalue]);
Nslog (@"************************************ *********************************");
}
Nslog (@"//////////////////////////////////// ///////////////////////////////////////" );
Nslog (@"//////////////////////////////////// ///////////////////////////////////////" );
}