IOS growth path-XML parsing with nsxmlparser

Source: Internet
Author: User
 

Parse the xml again and have some understanding. If there is something wrong, please point it out to the younger brother. Thank you!

<? Xml version = "1.0" encoding = "UTF-8"?> <Result> <meeting addr = "203"> <creator> Zhang Yi </creator> <member> <name> Zhang 'er </name> <age> 20 </age> </ member> <name> Zhang San </name> <age> 21 </age> </member> <name> Zhang Si </name> <age> 22 </age> </member> </meeting> <meeting addr = "204"> <creator> Li Yi </creator> <member> <name> Li 'er </name> <age> 20 </age> </member> <name> Lee San </name> <age> 21 </age> </member> <name> li Si </name> <age> 22 </age> </member> </meeting> </result>

1. Get the data in the xml file: store it in a string

// Bundle is a directory that contains the NSString * path = [[NSBundle mainBundle] pathForResource: @ "test" ofType: @ "xml"]; NSString * _ xmlContent = [[NSString alloc] initWithContentsOfFile: path encoding: NSUTF8StringEncoding error: nil];

Use NSXMLParser for parsing:

Brief description of NSXMLParser parsing 1. is the analysis of the sax method 2. you need to create an NSXMLParser instance (alloc) and a parser (initWithData :) to define a delegate for the parser (setDelegate :) run the parser (parser) ++ when parser initializes and executes the parse Statement ([parser parse]), the program will jump to the proxy method and take the first proxy method ++ 3. this resolution method is the first proxy method implemented using its proxy NSXMLParserDelegate: to start processing xml data, it will traverse the entire xml and identify the element node name-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict; second proxy method: that is, get the information data stored in the text node-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string; Third proxy method: store the information obtained from the second proxy method-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName: This is the parsing process. In this process, the three proxy methods will be repeatedly executed until the traversal is completed. In addition, the method (void) for starting the parsing) parserDidStartDocument :( NSXMLParser *) parser; Method for parsing end execution-(void) parserDidEndDocument :( NSXMLParser *) parser; this method is executed when a parsing error occurs-(void) parser :( NSXMLParser *) parser parseerroccurred :( NSError *) parseError;

Resolution start:

2. preparations:

NSXMLParser *parse=[[NSXMLParser alloc] initWithData:[_xmlContent dataUsingEncoding:NSUTF8StringEncoding]];        [parse setDelegate:self];        [parse parse];        [parse release];

3. Specific implementation of the three proxy methods:

// The first proxy method:-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qualifiedName attributes :( NSDictionary *) attributeDict {// determine whether it is meetingif ([elementName isinclutostring: @ "meeting"]) {// determine the attribute node if ([attributeDict objectForKey: @ "addr"]) {// obtain the value NSString * addr = [attributeDict objectForKey: @ "addr"] ;}// judge member if ([elementName isEqualToString: @ "member"]) {NSLog (@ "member"]) ;}// second proxy method:-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {// get the data in the text node. Because the following method needs to save the data obtained here, you need to define a global variable (modifiable string) // NSMutableString * element = [[NSMutableString alloc] init]; // The value is null to clear the previous value [element setString: @ ""]; [element appendString: string]; // string is the value of the obtained text node. It is obtained as long as it is a text node (including line feed ), then identify and distinguish} // The third proxy method in the next method:-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementNamenamespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {NSString * str = [[NSString alloc] initWithString: element]; if ([elementName isEqualToString: @ "creator"]) {NSLog (@ "creator = % @", str);} if ([elementName isinclutostring: @ "name"]) {NSLog (@ "name = % @", str);} if ([elementName isinclutostring: @ "age"]) {NSLog (@ "age = % @", str);} [str release];}

Note:

When parsing xml data,

Whenever an element node is encountered, the first proxy method is executed. If an attribute node exists, you can directly obtain the value in this method;


Every time a text node is encountered, the second proxy method is executed to obtain the value in the text node and distinguish it from the third method.

If it is a line break, it will also be obtained because the line break is also a text node, but the line break will not be obtained after an element node ends.

For example: (line break 1)

<Li> text node </li>

(Linefeed 2)


Each element node has a line break before and after it. At this time, only line break 1 is obtained, instead of Line Break 2.

4. handling error: print error

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{    NSLog(@"%@",[parseError description]);}

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.