[IOS multithreading & amp; network, ios Multithreading

Source: Internet
Author: User
Tags xml example

[IOS multithreading & Network, ios Multithreading
A. Basic XML knowledge1. What is xml?
Extensible Markup Language"
Like JSON, it is also a common data format for interaction.
It is also called XML Document)

XML example
<Videos>
<Video name = "Porn 01st" length = "30"/>
<Video name = "Porn 02nd" length = "19"/>
<Video name = "Part 2 of porn 03rd" length = "33"/>
</Videos> 2. xml syntax a common XML document generally consists of the following parts:

  • Document declaration
  • Element)
  • Attribute)
(1) The document declaration is at the beginning of the XML document. A document Declaration must be prepared to declare the type of the XML document.
Simplest statement
<? Xml version = "1.0"?>

Use the encoding attribute to describe the character encoding of the document
<? Xml version = "1.0" encoding = "UTF-8"?> (2) An element includes the start tag and end tag.
Element Content: <video> porn </video>
No element content: <video> </video>
Short for no element content: <video/>

A single element can be nested with several child elements (Cross Nesting is not allowed)
<Videos>
<Video>
<Name> Division 3, 01st </name>
<Length> 30 </length>
</Video>
</Videos>

The standard XML document only has one root element at most. Other elements are all spaces and line breaks in the child element of the root element XML, which are processed as specific content.
The content of the following two elements is different.
1st
<Video> Xiao Huang </video>

2nd
<Video>
Yellow Man
</Video> (3) attributes an element can have multiple attributes.
<Video name = "Porn 01st" length = "30"/>
The video element has two attributes: name and length.
Attribute values must be enclosed by double quotation marks "" or single quotation marks ''.

In fact, the information represented by properties can also be represented by sub-elements, such
<Video>
<Name> Division 3, 01st </name>
<Length> 30 </length>
</Video> 3. xml ParsingTo extract useful information from XML, you must learn to parse XML
Extract content from the name Element
<Name> Division 3, 01st </name>

Extract the value of the name and length attributes in the video element.
<Video name = "Porn 01st" length = "30"/>

There are two ways to parse XML
DOM: loads the entire XML file into the memory at a time, which is suitable for parsing small files.
SAX: Starting from the root element, one element and one element are parsed in sequence. It is suitable for parsing large files (1) xml in iOS. There are many ways to parse XML in iOS.
Apple native
NSXMLParser: simple to use

Third-party framework
Libxml2: pure C language. It is included in the iOS SDK by default and supports DOM and SAX parsing.
GDataXML: DOM-based parsing, developed by Google, based on libxml2

XML parsing method selection suggestions
Large files: NSXMLParser and libxml2
Small file: GDataXML A. NSXMLParserProcedure
// Input XML data and create a parser
NSXMLParser * parser = [[NSXMLParser alloc] initWithData: data];
// Sets the proxy to listen to the parsing process
Parser. delegate = self;
// Start Parsing
[Parser parse];

NSXMLParser adopts the SAX parsing method, which is characterized by event-driven. The proxy will be notified in the following situations.
When a Document is scanned
When the Element is scanned B. NSXMLParserDelegateCall (start parsing) when scanning the beginning of a document)
-(Void) parserDidStartDocument :( NSXMLParser *) parser

Called upon scanning to the end of the document (resolution completed)
-(Void) parserDidEndDocument :( NSXMLParser *) parser

It is called when an element is scanned to start (attributeDict stores the attribute of the element)
-(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict

Called at the end of an element Scan
-(Void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName
1 // 2 // HVWVideoViewController. m 3 // ParseXMLDemo 4 // 5 // Created by hellovoidworld on 15/1/26. 6 // Copyright (c) 2015 hellovoidworld. all rights reserved. 7 // 8 9 # import "HVWVideoViewController. h "10 # import" HVWVideo. h "11 # import" HVWVideoCell. h "12 13 @ interface HVWVideoViewController () <UITableViewDataSource, UITableViewDelegate, NSURLConnectionDataDelegate, NSXMLParserDelegate> 14 1 5/** cached data */16 @ property (nonatomic, strong) NSMutableData * data; 17 18/** cached video information */19 @ property (nonatomic, strong) NSMutableArray * videos; 20 21 @ end 22 23 @ implementation HVWVideoViewController 24 25-(void) viewDidLoad {26 [super viewDidLoad]; 27 28 // send request 29 NSURL * url = [NSURL URLWithString: @ "http: // 192.168.0.21: 8080/MyTestServer/video? Type = xml "]; 30 NSURLRequest * request = [NSURLRequest requestWithURL: url]; 31 NSURLConnection * conn = [NSURLConnection connectionWithRequest: request delegate: self]; 32 [conn start]; 33} 34 35 # pragma mark-Table view data source 36 37-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {38 return 1; 39} 40 41-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NS Integer) section {42 return self. videos. count; 43} 44 45-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {46 HVWVideoCell * cell = [HVWVideoCell cellWithTableView: tableView]; 47 cell. video = self. videos [indexPath. row]; 48 return cell; 49} 50 51 # pragma mark-UITableViewDelegate 52-(CGFloat) tableView :( UITableView *) tableView heightForRowA TIndexPath :( NSIndexPath *) indexPath {53 return 70; 54} 55 56 57 # pragma mark-NSURLConnectionDataDelegate 58-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {59 NSLog (@ "the network is busy. Please try again later! "); 60} 61 62-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {63 NSLog (@" start to accept data "); 64 self. data = [NSMutableData data]; 65} 66 67-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {68 NSLog (@ "receiving data ---> data Length: % d byte", data. length); 69 70 // accumulate data 71 [self. data appendData: data]; 72} 73 74-(void) connectionDidFinishLoading :( NSURLConnection *) connection {75 NSLog (@ ""); 76 77 // start to process data 78 NSXMLParser * parser = [[NSXMLParser alloc] initWithData: self. data]; 79 parser. delegate = self; 80 // start parsing 81 [parser parse]; 82} 83 84 # pragma mark-NSXMLParserDelegate 85/** xml document start */86-(void) parserDidStartDocument :( NSXMLParser *) parser {87 NSLog (@ "Start parsing xml"); 88} 89 90/** end of xml document */91-(void) parserDidEndDocument :( NSXMLParser *) parser {92 NSLog (@ "xml parsed"); 93} 94 95/** start parsing element */96-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI attributes :( NSString *) qName attributes :( NSDictionary *) attributeDict {97 NSLog (@ "Start parsing % @", elementName ); 98 99 if ([@ "videos" isEqualToString: elementName]) {// This will also be parsed to the root node 100 self. videos = [NSMutableArray array]; 101} else if ([@ "video" isEqualToString: elementName]) {// if it is a video node, start to extract 102 HVWVideo * video = [HVWVideo videoWithDict: attributeDict]; 103 [self. videos addObject: video]; 104} 105} 106 107/** end parsing element */108-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {109 NSLog (@ "Resolution completed % @", elementName); 110 111 // refresh data 112 [self. tableView reloadData]; 113} 114 115 @ end
C. GDataXML ConfigurationGDataXML is based on the libxml2 library. You must configure the following to import the libxml2 library.
Set the header file search path for libxml2 (in order to find all header files in the libxml2 library)
In Head Search PathAdd/usr/include/libxml2

Set link parameters (automatically linking libxml2 library)
In Other Linker Flags-Lxml2 is added. Because GDataXML is not ARC, you must set the compilation parameter 1-(void) connectionDidFinishLoading :( NSURLConnection *) connection {2 NSLog (@ ""); 3 4 // start to process data 5 // 1. load Document 6 GDataXMLDocument * doc = [[GDataXMLDocument alloc] initWithData: self. data options: 0 error: nil]; 7 8 // 2. obtain the root element 9 GDataXMLElement * root = [doc rootElement]; 10 11 // 3. parse all elements 12 NSArray * elements = [root elementsForName: @ "video"]; 13 14 // 4. encapsulate data 15 self. videos = [NSMutableArray array]; 16 for (GDataXMLElement * ele in elements) {17 HVWVideo * video = [[HVWVideo alloc] init]; 18 video. name = [ele attributeForName: @ "name"]. stringValue; 19 video. length = [ele attributeForName: @ "length"]. stringValue; 20 video. image = [ele attributeForName: @ "image"]. stringValue; 21 video. video = [ele attributeForName: @ "video"]. stringValue; 22 [self. videos addObject: video]; 23} 24 25 // refresh data 26 [self. tableView reloadData]; 27}

 

Related Article

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.