XML: Using an open source parsing class, Gdataxmlnode (adding it to the project), adding the Libxml2.dylib framework
Frequently Used methods:
1.-(ID) initwithxmlstring: (NSString *) str options: (unsigned int) mask error: (Nserror *) error
2.-(ID) initwithdata: (NSData *) data options: (unsigned int) mask error: (Nserror *) error
These two methods can convert the data of the NSString type or NSData class into an XML document that the Gdataxmlnode class can parse
3.-(gdataxmlelement *) rootelement returns all node information in Gdataxmlelement
4.-(Nsarray *) Elementsforname: (NSString *) name returns all nodes named name under the current node, and returns an array of values.
5.-(NSString *) StringValue returns the value in the middle of the node.
JSON: Using the Open source framework Sbjson, the extension of the NSString class, all NSString objects in our program can call the method.
Sbjson Method-(ID) Jsonvalue This method returns an ID class object because the JSON information is transmitted as a key value, so we will use Nsdictionary to receive the return value.
Two types of structures:
1. The collection of "key-value pairs". Objects (object), records (record), structure (struct), Dictionary (dictionary), hash Table (Hashtable), key list (keyed list), associative array (associative array).
2. An ordered list of values. Array.
JSON parsing
1 curly braces represent dictionaries
2 brackets (parentheses) represent the array
3 The left and right sides of the colon are keys and values (Key:value), respectively
4 data and data separated by commas
We create a file.xml on this machine, parse it, and take out age 35
JSON parsing
Read Student.xml files locally
NSString *jstr=[nsstring stringwithcontentsoffile:[[nsbundle mainbundle]pathforresource:@ "File" ofType:@ "JSON"] Encoding:nsutf8stringencoding error:nil];//Fetch to Path
To parse, because the outermost is the dictionary, so to use the dictionary type of object to connect
Nsdictionary *dic=[jstr Jsonfragmentvalue];
The following values are taken
Fetch the Hello key and fetch the contents of the array so that you can use the array to connect
Nsarray *arr=[dic objectforkey:@ "Hello"];
Take the array labeled 1, because the array is also a dictionary, so use a dictionary to pick up
Nsdictionary *dic2=[arr Objectatindex:1];
Takes the value of the dictionary key to age, because the value that is taken is a string, which is followed by a string.
NSString *jage=[dic2 objectforkey:@ "age"];
Output out of age
NSLog (@ "%@", jage);
//xml parsing
Also we create a file.xml file locally that contains:
<students>
<student>
<name>kellen</name>
<age>25</age>
<sex>M</sex>
</student>
<student>
<name>jack</name>
<age>35</age>
<sex>M</sex>
</student>
</students>
Step1: Read Student.xml files locally
Get the XML file for the directory
Nsstring*xmlpath=[[nsbundle mainbundle]pathforresource:@ "File" oftype:@ "xml";
Nsdata*xmldata=[[nsdata Alloc]initwithcontentsoffile:xmlpath];
Step2: Converts a string containing XML content into a Document object
Gdataxmldocument*xmldoc=[[gdataxmldocument alloc]initwithdata:xmldata options:0 Error:nil];
Step3: Get to the root node
Get root node (students)
Gdataxmlelement*rootelement=[xmldoc RootElement];
STEP4: Traversing node information
Gets the node under the root node (student)
Nsarray*students=[rootelement elementsforname:@ "Student"];
NSLog (@ "%@", students);
Use Forin to traverse all data in XML
For (Gdataxmlelement*stu in students) {
Gets the properties of the name node
Gdataxmlelement*nameelement=[[stu elementsforname:@ "name"]objectatindex:0];
Nsstring*name=[nameelement stringvalue];//Get Nameelement
NSLog (@ "Student name is:%@", name);
Gets the properties of the age node
Gdataxmlelement*ageelement=[[stu elementsforname:@ "age"]objectatindex:0];
Nsstring*age=[ageelement StringValue];
NSLog (@ "User Age was:%@", age);
}
}
JSON parsing and XML parsing