1. Introduction to XML:
?了解: The full name of XML is Extensible Markup Language translated as "Extensible Markup Language"; As with JSON, it is commonly used as a data format for interaction, often referred to as an XML document.
examples of XML documents :
2, XML Syntax Introduction: 2-1, xml-document declaration:
2-2. xml-elements (Element):
2-3, xml-elements Note:
2-4. xml-attribute (Attribute):
3. XML parsing scheme:
?了解: XML has 2 kinds of parsing: ① DOM : The whole XML document is loaded into memory at once, it is more suitable for parsing small files; ② SAX : Starting from the root element, in order to parse an element down, it is more suitable for parsing large files.
3-1. XML parsing scheme:
3-2, the choice of XML parsing mode:
4, Nsxmlparser Analysis:
?了解: Nsxmlparser is parsing xml based on sax.
4-1, nsxmlparser resolution steps:
-
Step one: Create a nsxmlparser parser, a Nsxmlparser object representing a parser;
1.// 6. 创建解析器
2.NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
-
Step two: Set up the proxy object of the parser, the proxy object adheres to <NSXMLParserDelegate> the protocol and implements the corresponding analytic method;
1.// 7. 设置代理
2.parser.delegate = self;
-
Step three: The parser calls the parse method to begin parsing.
1.// 8. 开始解析
2.[parser parse];
4-2, Nsxmlparser Agent method:
4-3. Sample program:
1. #pragma mark-nsxmlparserdelegate
2./**
3. Start parsing the XML document, calling only once
4. */
5.-(void) Parserdidstartdocument: (Nsxmlparser *) parser{
6. NSLog (@ "%s", __func__);
9.3
8.
9./**
10. Start parsing the XML element, how many times it will be called when there are elements in the XML file
11. */
12.-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString *) QName attributes: (nsdictionary<nsstring *,nsstring *> *) attributedict{
NSLog (@ "%s", __func__);
14.
15.//1. Replace property name
[Hhmovie mj_setupreplacedkeyfrompropertyname:^nsdictionary *{
@{@ return "id": @ "id"};
18.}];
19.
20.//2. Determines whether it is a root element because the content of the root element is a child element; Therefore, cannot be converted to a model
if ([elementname isequaltostring:@ "Videos"]) {
A. return;
23.}
24.
25.//3. Create a model
Hhmovie *movie = [Hhmovie mj_objectwithkeyvalues:attributedict];
27.
28.//4. Add to model array
[Self.movies Addobject:movie];
30.}
31.
32./**
33. Parse out elements in XML
34. */
35.-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qname{
NSLog (@ "%s", __func__);
37.}
38.
39./**
40. Parse the entire XML document, and the last Call
41. */
42.-(void) Parserdidenddocument: (Nsxmlparser *) parser{
NSLog (@ "%s", __func__);
44.
45.//1. Refresh table in main thread
Dispatch_async (Dispatch_get_main_queue (), ^{
[Self.tableview Reloaddata];
48.});
49.}
5, gdataxml5-1, analytic environment configuration:
5-2, Gdataxml Common class:
5-3. Sample program:
1.//MARK:-Gdataxmlnode parsing xml file
2.//1. Creates an XML Document object, and a Gdataxmldocument object represents an XML document
3. Gdataxmldocument *doc = [[Gdataxmldocument alloc] Initwithdata:data options:0 Error:nil];
4.
5.//2. Get root element
6. Gdataxmlelement *rootelement = [Doc rootelement];
7.
8.//3. Gets all child elements in the root element based on the element name
9. Nsarray *elements = [rootelement elementsforname:@ "video"];
10.
11.//4. Traverse
[Elements enumerateobjectsusingblock:^ (gdataxmlelement *element, Nsuinteger idx, BOOL * _nonnull stop) {
13.//5. Create a model
Hhmovie *movie = [[Hhmovie alloc] init];
15.
16.//6. Gets the property value based on the attribute name in the element
Movie.id = [element attributeforname:@ "id"].stringvalue.integervalue;
Movie.image = [Element attributeforname:@ "image"].stringvalue;
Movie.length = [Element attributeforname:@ "Length"].stringvalue;
Movie.url = [element attributeforname:@ "url"].stringvalue;
Movie.name = [Element attributeforname:@ "name"].stringvalue;
22.
23.//7. Add to model array
[Self.movies Addobject:movie];
25.
26.//8. Refresh Table
Dispatch_async (Dispatch_get_main_queue (), ^{
[Self.tableview Reloaddata];
29.});
30.}];
iOS core note-Network programming-xml