XML data parsing (system method) and xml data parsing method
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 one </> ends.
The parsed content is:
<Users> <user id = "001"> <name> Zhang San </name> <password> 123456 </password> <nickname> Xiao Zhang </nickname> <description> 1111111111 </description> </user> <user id = "002"> <name> JOHN </name> <password> 2222222 </password> <nickname> JOHN </nickname> <description> Li Si is a good student </description> </user> <user id = "003"> <name> Zhang San </name> <password> 88888888 </password> <nickname> 5th </nickname> <description> 1234567890 </description> </user> </users>
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 <Foundation/Foundation. h> # import "user. h "@ interface userParser: NSObject <NSXMLParserDelegate> // array for storing information @ property (nonatomic, retain) NSMutableArray * array; // instance @ property (nonatomic, retain) user * people; // create a string to save 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]; // <user id = "3"> _ 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.