IOS network analysis-(five xml parsing), iosxml

Source: Internet
Author: User

IOS network analysis-(five xml parsing), iosxml

XML

NSXMLParser Parsing Method

The NSXMLParser we use is parsed using the SAX method.

SAX (Simple API for XML)

  • It can only be read, cannot be modified, and can only be accessed in sequence. It is suitable for parsing large XML files with fast resolution speed.
  • XML is often used to process large amounts of data to achieve data access in heterogeneous systems and cross-platform
  • From the beginning of the document, move each node to locate a specific node.

DOM (Document Object Model)

  • Not only can read, but can also modify, and achieve random access, the disadvantage is that the resolution speed is slow, suitable for parsing small documents
  • Common applications and small configuration XML for easy operation
  • Creates a type description for the file nodes loaded into the memory to present a horizontally moving, potentially huge tree structure.
  • Generating node tree in memory is expensive

XmlParser parsing process

NSXMLParser parsing process

1. Create an NSXMLParser instance and input XML data received from the server.

2. Define the parser proxy

3. parser Parsing

4. parse XML data using the parsing proxy method

Use the Protocol <NSXMLParserDelegate> to parse a document using XML to implement its proxy method.

// 1. start parsing an element and traverse the entire XML to identify the element node name-(void) parser: didStartElement: namespaceURI: qualifiedName: attributes: // 2. the text node obtains the information data stored in the text node. For big data, it may be received multiple times! To save memory overhead-(void) parser: foundCharacters: // 3. end a node and store the information obtained from parser: foundCharacters: method-(void) parser: didEndElement: namespaceURI: qualifiedName: Note: During the parsing process, the preceding three methods will be repeatedly executed until the traversal is complete. // 4. start parsing XML document-(void) parserDidStartDocument: // 5. parse XML document end-(void) parserDidEndDocument: // 6. parsing error-(void) parser: parseerroccurred:

 

During XMLParser parsing, you also need to implement the NSXMLParserDelegate proxy method

If an XML file contains multiple objects during parsing, pay attention to the following points in order to correctly parse the data in the document:

1. Is the current parsed object or element value?

If it is an object, you need to determine the first object, the second object, the third object ......, If the number is N, the object value for the N-1 needs to be taken out and saved.

For element values, the parsed data must be assigned to an attribute corresponding to the object.

2. Each time a character is read during the parsing process, all the characters that must be parsed must be concatenated and combined. Used to determine whether the parsed object or element name is used. Or element value.

Code is coming ---

/// ViewController. m // CX-XML resolution /// Created by ma c on 16/3/18. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () <NSXMLParserDelegate> @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; NSURL * url = [NSURL URLWithString: @" http: // 192.168.1.200/train. xml "]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mai NQueue] completionHandler: ^ (handler * _ Nullable response, NSData * _ Nullable data, NSError * _ Nullable connectionError) {// xml parsing NSXMLParser * parse = [[NSXMLParser alloc] handler: data]; parse. delegate = self; [parse];} // open the document-(void) parserDidStartDocument :( NSXMLParser *) parser {NSLog (@ "open the document, start Parsing ");} // start searching the start tag-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elem EntName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary <NSString *, NSString *> *) attributeDict {NSLog (@ "Start Element: % @", elementName); NSLog (@ "attribute % @", attributeDict);} // obtain the TAG content-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {NSLog (@ "get content: % @", string) ;}// search end tag-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namesp AceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {NSLog (@ "end tag: % @", elementName);} // end of searching for a document-(void) parserDidEndDocument :( NSXMLParser *) parser {NSLog (@ "Resolution completed");}/* The result is too long and the part is truncated. 10:25:21. 734 CX-XML resolution [4043: 238219] Open the document, start parsing 10:25:21. 735 CX-XML resolution [4043: 238219] Start Element: dataSet 10:25:21. 736 CX-XML resolution [4043: 238219] properties {xmlns = "http://WebXml.com.cn/";} 10:25:21. 736 CX-XML resolution [4043: 238219] Get content: 10:25:21. 736 CX-XML resolution [4043: 238219] Start Element: diffgr: diffgram 10:25:21. 737 CX-XML resolution [4043: 238219] properties {"xmlns: diffgr" = "urn: schemas-microsoft-com: xml-diffgram-v1"; "xmlns: msdata" = "urn: schemas-microsoft-com: xml-msdata ";} 10:25:21. 737 CX-XML resolution [4043: 238219] Get content: 10:25:22. 074 CX-XML resolution [4043: 238219] end label: diffgr: diffgram 10:25:22. 074 CX-XML resolution [4043: 238219] Get content: 10:25:22. 074 CX-XML resolution [4043: 238219] end label: dataSet 10:25:22. 074 CX-XML resolution [4043: 238219] resolution completed */@ end

 

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.