ios-data Persistence base-json and XML data parsing

Source: Internet
Author: User

Basic concepts of parsing

"Parsing": extracting data from pre-defined format strings

The premise of the resolution: in advance to agree on a good format, data providers to provide data in a format, data acquisition by the format of data acquisition

Common parsing for iOS development: XML parsing, JSON parsing

I. XML data structure

Basic concepts of parsing

"Parsing": extracting data from pre-defined format strings

The premise of the resolution: in advance to agree on a good format, data providers to provide data in a format, data acquisition by the format of data acquisition

Common parsing for iOS development: XML parsing, JSON parsing

XML data structure

XML parsing
SAX Method Parsing
From the top down,? A little bit. Read
Good performance
Apple recommends the use of
DOM? Way
? Once the XML document is read in a tree structure. into memory
How much is the memory consumption?
In iOS development default does not support Dom? Method parsing
Some third party frameworks implement Dom? Way
Only for small XML? Parsing of files
Kissxml & Gdat

XML Data Structure Basic Concepts

Xml:extensible Markup Language (Extensible Markup Language), one of the mainstream formats that can be used to store and transfer data in one of the formats that can be used to store and transfer data

Features of the XML data format

1. Data exchange

2. Content Management

3, as a configuration file

syntax for XML data structures

1. Statement

2, the node uses a pair of tags to represent

3, the root node is the starting node, only one. Nodes can be nested

4, nodes can have values, stored in a pair of tags

How to parse XML

There are two ways to do this:

1. Sax parsing

Sax:simple API for XML. Parse the data line by row, based on the event-driven parsing method. (with protocol callback mechanism)

Sax parses XML, is based on the event notification pattern, while reading the XML document side processing, do not have to wait until the entire document is loaded before taking action, the SAX parser will detect the entire XML tree structure, your code will control where it stops, the use of which data and other things. That is, sax is highly controllable and takes up little memory and is suitable for extracting part of the data. When an object that needs to be processed is encountered during the read parsing process, a notification is issued to process it, and if an error occurs in the XML format, the preceding data is extracted and the data behind the error is not displayed.

The Nsxmlparse class is an XML parsing class that comes with iOS. Parsing data using sax

The parsing process is callback by the Nsxmlparserdelegate protocol method

Parsing process: Start tag---value--end tag

The implementation of the code is, first we create an XML document, and introduce the file, as follows:

Next we build a table to try the controller (Tableviewcontroller)

Defines a private property array DataSource used to store model data and Starttag properties for staging the start tag

At the same time, get the file data in the method viewdidload and specify the proxy <nsxmlparserdelegate>//parser protocol, and register the cell type for the corresponding reuse identifier.

The Nsxmlparserdelegate parser protocol has the following methods:

When XML parses file data one by one, because the start tag and end tag name are the same, when parsing to the end tag, in order to avoid parsing the contents of the end tag to overwrite the corresponding property in the Model object, the start tag is set to nil within the method of parsing the end tag

Inside each method We print the method name at the same time, so that we can clearly see the order of execution of the method on the output side,

Also establish a reconstruction mechanism within the table setup method

By setting the appropriate number of rows and partitions, we can display the following results on the emulator:

2. DOM Tool parsing

Dom:document object Model (Document object models). Parsing requires that the XML file be read into the whole, and the XML is structured into a tree, using the tree structure to read the relevant data, find a specific node, and then read or write to the node. His main advantage is to achieve a simple, read-write balance, the disadvantage is to compare the memory, because he wants to read the entire XML document into memory, the larger the file, this disadvantage is more obvious. The wrong location is marked in the input box when there is an error in the contents of the file

Gdataxmlnode is a new Kaiyuan XML parsing class provided by Google, Libxml2.dylib is objective-c encapsulated, so you need to import LIBXML2 before using Gdataxml .

iOS contains a C language dynamic link library libxml2.dylib, resolution faster than Nsxmlparser

Second, JSON data structure

JSON data Structure Basic Concepts

Javascript Object Notation, the Lightweight Data Interchange format, is a completely language-independent text format, known as the ideal data exchange language, easy to read and portable, but also easy to parse, because JSON parsing is convenient and fast, And the same data is smaller with JSON editing, so we use JSON parsing more commonly in iOS.

There are two kinds of structure for JSON documents: objects, data

Object: Starting with "{", Ending with "}", is a collection of "name/value" pairs. The name and value are separated by ":". Multiple "name/value" pairs are separated by ",". Similar to the dictionary in OC.

Array: Ends with "[", "]", and the middle is the data. The data is split with ",".

Data types in JSON: string, numeric, BOOL, object, array.

For example:

JSON–OC Conversion Table

Functions of JSON data structures

1. Data exchange

2. Content Management

3. Configuration files

JSON parsing scheme

In iOS, there are 4 common parsing scenarios for JSON
Third-party frameworks: Jsonkit, Sbjson, Touchjson (performance from left to right, worse)
Apple native (comes with): Nsjsonserialization (Best performance)

Here is the Nsjsonserialization parsing process:

XML Data Structure Basic Concepts

Xml:extensible Markup Language (Extensible Markup Language), one of the mainstream formats that can be used to store and transfer data in one of the formats that can be used to store and transfer data

Features of the XML data format

1. Data exchange

2. Content Management

3, as a configuration file

syntax for XML data structures

1. Statement

2, the node uses a pair of tags to represent

3, the root node is the starting node, only one. Nodes can be nested

4, nodes can have values, stored in a pair of tags

How to parse XML

There are two ways to do this:

1. Sax parsing

Sax:simple API for XML. Parse the data line by row, based on the event-driven parsing method. (with protocol callback mechanism)

Sax parses XML, is based on the event notification pattern, while reading the XML document side processing, do not have to wait until the entire document is loaded before taking action, the SAX parser will detect the entire XML tree structure, your code will control where it stops, the use of which data and other things. That is, sax is highly controllable and takes up little memory and is suitable for extracting part of the data. When an object that needs to be processed is encountered during the read parsing process, a notification is issued to process it, and if an error occurs in the XML format, the preceding data is extracted and the data behind the error is not displayed.

The Nsxmlparse class is an XML parsing class that comes with iOS. Parsing data using sax

The parsing process is callback by the Nsxmlparserdelegate protocol method

Parsing process: Start tag---value--end tag

The implementation of the code is, first we create an XML document, and introduce the file, as follows:

Next we build a table to try the controller (Tableviewcontroller)

Defines a private property array DataSource used to store model data and Starttag properties for staging the start tag

At the same time, get the file data in the method viewdidload and specify the proxy <nsxmlparserdelegate>//parser protocol, and register the cell type for the corresponding reuse identifier.

The Nsxmlparserdelegate parser protocol has the following methods:

When XML parses file data one by one, because the start tag and end tag name are the same, when parsing to the end tag, in order to avoid parsing the contents of the end tag to overwrite the corresponding property in the Model object, the start tag is set to nil within the method of parsing the end tag

Inside each method We print the method name at the same time, so that we can clearly see the order of execution of the method on the output side,

Also establish a reconstruction mechanism within the table setup method

By setting the appropriate number of rows and partitions, we can display the following results on the emulator:

2. DOM Tool parsing

Dom:document object Model (Document object models). Parsing requires that the XML file be read into the whole, and the XML is structured into a tree, using the tree structure to read the relevant data, find a specific node, and then read or write to the node. His main advantage is to achieve a simple, read-write balance, the disadvantage is to compare the memory, because he wants to read the entire XML document into memory, the larger the file, this disadvantage is more obvious. The wrong location is marked in the input box when there is an error in the contents of the file

Gdataxmlnode is a new Kaiyuan XML parsing class provided by Google, Libxml2.dylib is objective-c encapsulated, so you need to import LIBXML2 before using Gdataxml .

iOS contains a C language dynamic link library libxml2.dylib, resolution faster than Nsxmlparser

Second, JSON data structure

JSON data Structure Basic Concepts

Javascript Object Notation, the Lightweight Data Interchange format, is a completely language-independent text format, known as the ideal data exchange language, easy to read and portable, but also easy to parse, because JSON parsing is convenient and fast, And the same data is smaller with JSON editing, so we use JSON parsing more commonly in iOS.

There are two kinds of structure for JSON documents: objects, data

Object: Starting with "{", Ending with "}", is a collection of "name/value" pairs. The name and value are separated by ":". Multiple "name/value" pairs are separated by ",". Similar to the dictionary in OC.

Array: Ends with "[", "]", and the middle is the data. The data is split with ",".

Data types in JSON: string, numeric, BOOL, object, array.

For example:

JSON–OC Conversion Table

Functions of JSON data structures

1. Data exchange

2. Content Management

3. Configuration files

JSON parsing scheme

In iOS, there are 4 common parsing scenarios for JSON
Third-party frameworks: Jsonkit, Sbjson, Touchjson (performance from left to right, worse)
Apple native (comes with): Nsjsonserialization (Best performance)

Here is the Nsjsonserialization parsing process:

ios-data Persistence base-json and XML data parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.