HTTP request returns XML
First, XML simple introduction
XML: Full name is extensible Markup Language, translated "Extensible Markup Language"
Like JSON, it is also a commonly used data format for interaction, often called an XML document (XML documents).
XML Example
<videos>
<video name= "Little yellow No. 01" length= "/>"
<video name= "Small yellow No. 02" length= "/>"
<video name= "Small yellow No. 03" length= "/>"
</videos>
II. syntax of XML
1. Brief description
A common XML document typically consists of the following sections
Document Declaration
Elements (Element)
Properties (Attribute)
2. Document Declaration
At the very front of the XML document, you must write a document declaration that declares the type of the XML document
The simplest statement
<?xml version= "1.0"?>
Use the Encoding property to describe the character encoding of a document
<?xml version= "1.0" encoding= "UTF-8"?>
3.XML syntax – elements (Element)
An element includes a start tag and an end tag
own element content:<video> Yellow man </video>
No element content:<video></video>
Shorthand:<video/> with no element content
An element can nest several child elements (no cross-nesting)
<videos>
<video>
<name> Small Yellow No. 01 Department </name>
<length>30</length>
</video>
</videos>
Attention:
(1) The canonical XML document has a maximum of 1 root elements, and the other elements are descendants of the root element
(2) All spaces and line breaks in XML are treated as concrete content
The contents of the following two elements are not the same
1th One
<video> Small yellow people </video>
2nd One
<video>
Minions
</video>
4.XML syntax – attributes (Attribute)
An element can have multiple properties
<video name= "Little yellow No. 01" length= "/>---->video element has name and length two properties
Note: Attribute values must be enclosed in double quotation marks "" or single quotes "
In fact, the information represented by a property can also be represented by a child element, such as
<video>
<name> Small Yellow No. 01 Department </name>
<length>30</length>
</video>
Third, the parsing of XML
1. Brief description
To extract useful information from XML, you have to learn to parse the XML
Extract the contents of the name element
<name> Small Yellow No. 01 Department </name>
Extract the value of the name and length property in the video element
<video name= "Little yellow No. 01" length= "/>"
There are 2 ways to parse XML
(1) DOM: Loading the entire XML document into memory at once, which is more suitable for parsing small files
(3) SAX: Starting from the root element, in order an element of an element down, more suitable for parsing large files
Parsing of XML in 2.IOS development
In iOS, there are many ways to parse XML
(1) Apple native Nsxmlparser:sax method analysis, easy to use
(2) Third party framework
1) LIBXML2: pure C language, which is included by default in the iOS SDK while supporting DOM and sax parsing
2) Gdataxml:dom method Analysis, developed by Google, based on LIBXML2
Suggestions for choosing XML parsing methods:
Large files: Nsxmlparser, LIBXML2
Small files: gdataxml
Parsing XML data with Gdataxml
Parsing with Nsxmlparser
Proxy approach to implement <NSXMLParserDelegate>