XML for iOS advanced programming, JSON data parsing, and iosjson
Basic Concepts
So-called "resolution": engage in extracting data from a specified format string
Prerequisites for parsing: The format is agreed in advance, the data provider provides data in the format, and the data recipient obtains data in the format.
Common parsing for iOS development: XML parsing and JSON Parsing
I. XML Data Structure
XML Data StructureBasic Concepts
XML: Extensible Markup language (Extensible Markup language), one of the mainstream formats, can be used to store and transmit one of the data formats, can be used to store and transmit data
XML data format
1. Data Exchange
2. Content Management
3. Used as a configuration file
XML Data Structure syntax
1. Statement
2. nodes are represented by a pair of labels.
3. The root node is the Start Node and only one. Nodes can be nested
4. nodes can have values and are stored in a pair of labels.
How to parse XML
There are two methods:
1. SAX Parsing
SAX: Simple API for XML. parse data row by row based on the event-driven parsing method. (Protocol callback mechanism is adopted)
The XML parsing method is based on event notification. The process is performed while reading the XML document. You do not have to wait until the entire document is loaded. The SAX Parser detects the entire XML tree structure, your code will control where it stops and what data it uses. That is to say, SAX is highly controllable and occupies a small amount of memory. It is suitable for extracting some data. When an object to be processed is encountered during the read and resolution process, a notification is sent to process the object. If an error occurs in the XML format, the preceding data is extracted, the data after the error cannot be displayed.
The NSXMLParse class is an XML parsing class that comes with iOS. Parse data using SAX
The parsing process is called back by the NSXMLParserDelegate protocol method.
Resolution Process: start tag-> value-> end tag-> Value
The implementation process of the Code is as follows:
Next, we create a table to try the controller (TableViewController)
Define a private attribute array datasource to store model data and startTag to store the start tag
At the same time, obtain the file data in the ViewDidLoad method and specify the proxy <NSXMLParserDelegate> // parser protocol, and register the cell type for the corresponding reuse identifier.
The NSXMLParserDelegate parser protocol contains the following methods:
When XML parses file data one by one, because the start tag and end tag name are the same, when the end tag is parsed, to avoid overwriting the corresponding attributes in the model object after the end tag, set the start tag to nil in the method of parsing the end tag.
In each method, we print the method name at the same time, so that we can clearly see the execution sequence of the method at the output end,
At the same time, a reconstruction mechanism is established inside the table setting method.
After setting the corresponding number of rows and partitions, the following results can be displayed on the simulator:
2. DOM tool Parsing
DOM: Document Object Model (Document Object Model ). During parsing, you need to read the XML file as a whole and convert the XML structure into a tree. When using the tree structure, you can read the relevant data, find a specific node, and then read or write the node. Its main advantage is its simplicity and read/write balance. Its disadvantage is that it occupies the memory because it reads the entire xml file into the memory. The larger the file, the more obvious this disadvantage is. When an error occurs in the file content, the incorrect position is marked in the input box.
GDataXMLNode is an open-source XML parsing class provided by Google. It encapsulates libxml2.dylib in Objective-C. Therefore, before using GDataXML, you must first import libxml2.
IOS contains a C language dynamic link library libxml2.dylib, which is faster than NSXMLParser.
Ii. JSON Data Structure
JSON Data StructureBasic Concepts
Javascript Object Notation, lightweight data exchange format, uses a completely language-independent text format, is called the ideal Data Exchange Language, easy to read and portable, at the same time, it is also easy to parse. Because JSON Parsing is convenient and fast, and the memory occupied by JSON editing for the same data is smaller, we use JSON parsing in iOS to make it more common.
JSON documents have two structures: objects and data.
Object: end with "{" Start, end with "}", which is a set of "name/value" pairs. Names and values are separated. Multiple "name/value" pairs are separated. Similar to the dictionary in OC.
Array: starts with "[" and ends with "]", with data in the middle. Data is separated by commas.
Data Types in JSON: String, value, BOOL, object, and array.
For example:
JSON-OC conversion table
Functions of JSON Data Structure
1. Data Exchange
2. Content Management
3. Configuration File
JSON resolution scheme
In iOS, there are four common JSON resolution solutions.
Third-party frameworks: JSONKit, SBJson, and TouchJSON (performance from left to right, the worse)
Apple native (built-in): NSJSONSerialization (Best Performance)
The NSJSONSerialization parsing process is as follows: