IOS growth path-parsing xml data

Source: Internet
Author: User

Xml data to be parsed:

<?xml version="1.0" encoding="UTF-8"?><Data><Movie><title>good lucky to you</title><box>111</box>        <summary>This is a story</summary></Movie>    <Movie><title>hello</title><box>99</box>        <summary>oh,yes</summary></Movie>        <Movie><title>who am i</title><box>22</box>        <summary>chenglong</summary></Movie></Data>

Through the process and program described in the previous blog "Get network data", we can get network data, you can also pass the data to the method for parsing the data in this process,

// Customize a PraxMovie method. When the parameter is set, NSDate data is used to pass the received data-(void) PraxMovie :( NSData *) _ data {// NSXMLParser parses data in xml format. initialize it here and assign NSXMLParser * parser = [[NSXMLParser alloc] initWithData: _ data]; parser. delegate = self; [parser parse];}

Before parsing, we need to clear the previous data, because we sometimes run the program multiple times, so we need to delete the old data.

(Note: In this program, we use the Movie and MovieList classes. Movie stores three attributes [Movie name, box office, Introduction]. used to store the three pieces of data obtained after parsing xml,

The MovieList class defines an array that can be changed to store the Movie object, add, delete, and query the number of Movie objects in the array)

// Preparations before parsing-(void) parserDidStartDocument :( NSXMLParser *) parser {// remove all, clear MovieList * xmlMovielist; [self. xmlMovielist. moviearray removeAllObjects]; [self. tempString setString: @ ""];}

Query all nodes and obtain the data in each node.

// Query the node and alloc generates a Movie object for storing data-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qualifiedNameattributes :( NSDictionary *) attributeDict {if ([elementName isEqualToString: @ "Movie"]) {self. newwMovie = nil; self. newwMovie = [[Movie alloc] init]; [self. tempString setString: @ ""];} else {[self. tempString setString: @ ""] ;}} // Locate the node data and pass it through the NSString object-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {if (! [String isEqualToString: @ ""]) {[self. tempString appendString: string] ;}}

Then, differentiate the obtained nodes and store the obtained data in different Movie attributes.

// Save data-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) Based on the queried node *) qName {if ([elementName isinclutostring: @ "Movie"]) {// store it in the array [self. xmlMovielist Addmovie: self. newwMovie];} else if ([elementName isinclutostring: @ "title"]) {// Movie obtains the title attribute self. newwMovie. title = [NSString stringWithString: self. tempString];} else if ([elementName isinclutostring: @ "box"]) {// Movie boxOfficeGross attribute int temp = [[NSString stringWithString: self. tempString] intValue]; self. newwMovie. boxOfficeGross = [NSNumber numberWithInt: temp];} else if ([elementName isinclutostring: @ "summary"]) {// Movie obtains the summary attribute self. newwMovie. summary = [NSString stringWithString: self. tempString] ;}}

After the last node is parsed, the resolution is complete,

// Resolution end-(void) parserDidEndDocument :( NSXMLParser *) parser {// traverses the array and outputs the data of each node for (id tmp in self. xmlMovielist. moviearray) {NSLog (@ "tmp = % @", tmp);} self. newwMovie = nil; self. tempString = nil ;}

Resolution Process:

First, execute the method to query the node, then execute the method to obtain the data in the node, and then execute the method to differentiate nodes to save the data,

<--------------------------

-(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName

NamespaceURI :( NSString *) namespaceURI

QualifiedName :( NSString *) qualifiedName

Attributes :( NSDictionary *) attributeDict

To -------

-(Void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string

Then to -------

-(Void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName

NamespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName

------------------------------>

Each node executes the three methods once. After a node is traversed, it re-executes the process to traverse the next node until all the nodes are traversed. Finally, execute:

-(Void) parserDidEndDocument :( NSXMLParser *) parser

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.