Code sharing of system methods for XML data parsing

Source: Internet
Author: User
XML is a self-describing data exchange format. It is a very important data exchange format and has been used in various computer languages for many years. XML is similar to HTML and has a pair of tag pairs. the format is more rigorous than HTML. it starts with & lt; & gt; and ends with & lt; & gt. XML is a self-describing data exchange format. It is a very important data exchange format and has been used in various computer languages for many years. XML is similar to HTML and has a pair of tag pairs. the format is more rigorous than HTML. one <> starts and ends.

The parsed content is:

     
          
   
    
Zhang San
           
   
    
123456
           
   
    
Xiao Zhang
           
   
    
1111111111
       
           
          
   
    
Zhang San
           
   
    
2222222
           
   
    
John
           
   
    
Li Si is a good student.
       
           
          
   
    
Zhang San
           
   
    
88888888
           
   
    
5th
           
   
    
1234567890
       
       
 

Data has been read and stored in data in advance.
Create a class inherited from NSObject and named userInfo

@property(nonatomic,copy)NSString *uID;@property(nonatomic,copy)NSString *name;@property(nonatomic,copy)NSString *password;@property(nonatomic,copy)NSString *nickname;@property(nonatomic,copy)NSString *description;

Create a new class, which also inherits from NSObject and is named userParser to read xml files.

UserParser. h

# Import
 
  
# Import "user. h" @ interface userParser: NSObject
  
   
// Array for storing information @ property (nonatomic, retain) NSMutableArray * array; // instance @ property (nonatomic, retain) user * people; // create a string to save the information @ property (nonatomic, copy) NSMutableString * buffer; // read the xml file-(void) parserWithString :( NSString *) string; @ end
  
 

Userparser. m

# Import "userParser. h "@ implementation userParser-(void) parserWithString :( NSString *) string {// use the system XML Parsing method NSXMLParser * parser = [[NSXMLParser alloc] initWithData: [string dataUsingEncoding: NSUTF8StringEncoding]; // sets the proxy parser. delegate = self; // start parsing [parser parse];} # pragma mark --- NSXMLParserDelegate --- // start parsing-(void) parserDidStartDocument :( NSXMLParser *) parser {NSLog (@ "it begin");} // resolution end-(void) parserDidEndDocument :( NSXMLParser *) parser {NSLog (@ "it is over "); for (int I = 0; I <_ array. count; I ++) {user * user = _ array [I]; NSLog (@ "% @, % @", user. uID, user. name, user. password, user. nickname, user. description) ;}/// start tag // attributeDict tag attribute-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict {if ([elementName isinclutostring: @ "users"]) {_ array = [[NSMutableArray alloc] init];} else if ([elementName isinclutostring: @ "user"]) {_ people = [[user alloc] init]; //
 
  
_ People. uID = [attributeDict objectForKey: @ "id"];} else if ([elementName isinclutostring: @ "name"]) {_ buffer = [[NSMutableString alloc] init];} else if ([elementName isEqualToString: @ "password"]) {_ buffer = [[NSMutableString alloc] init];} else if ([elementName isenttostring: @ "nickname"]) {_ buffer = [[NSMutableString alloc] init];} else if ([elementName isinclutostring: @ "description"]) {_ buffer = [[NSMutableString alloc] init];} // end tag-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {if ([elementName isinclutostring: @ "name"]) {_ people. name = _ buffer;} else if ([elementName isinclutostring: @ "password"]) {_ people. password = _ buffer;} else if ([elementName isinclutostring: @ "nickname"]) {_ people. nickname = _ buffer;} else if ([elementName isenttostring: @ "description"]) {_ people. description = _ buffer;} else if ([elementName isinclutostring: @ "user"]) {[_ array addObject: _ people] ;}// read content-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {// NSCharacterSet string combination, take the space in the string [_ buffer appendString: [string character: [NSCharacterSet character];}
 

In viewcontroller, create the userparser instance object and use the parserWithString method to parse data.

userParser *uparser=[[userParser alloc]init];[uparser parserWithString:user];

Note: Separating data parsing from Interface Display helps reduce program coupling and also conforms to the MVC programming mode.

The above is the detailed content shared by the code for the system method of XML data parsing. For more information, see other related articles in the first PHP community!

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.