Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How IOS parses XML files

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how IOS parses XML files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

NSXMLParser is used to parse here, which is the xml parsing library that comes with apple.

Add a xml file to Xcode:

001 John Reminder Don't forget the meeting! 002 Jack cc how are you! 003 Tom bb I am fine!

Read it first.

NSString* path = [[NSBundle mainBundle] pathForResource:@ "shop" ofType:@ "xml"]; NSData * data = [[NSData alloc] initWithContentsOfFile:path options: (NSDataReadingMappedIfSafe) error:nil]; NSXMLParser * parser= [[NSXMLParser alloc] initWithData:tmpdata]; [parser setDelegate:self]; [parser parse]; [parser release]; [self parseShopListFromResponse:data]

Then parse:

# pragma mark NSXMLParser delegate methods- (void) parser: (NSXMLParser *) parser didStartElement: (NSString *) elementName namespaceURI: (NSString *) namespaceURI qualifiedName: (NSString *) qName attributes: (NSDictionary *) attributeDict {self.currentTag = elementName; if ([elementName isEqualToString:@ "root"]) {self.tmpList = [[NSMutableArray alloc] init] } else if ([elementName isEqualToString:@ "Shop"]) {self.tmpShop = [[ShopData alloc] init]; NSArray* array = [attributeDict allKeys]; NSString* key = [array lastObject]; NSString*s = [attributeDict objectForKey:key]; self.tmpShop.info = s }}-(void) parser: (NSXMLParser *) parser foundCharacters: (NSString *) string {if (self.currentString = = nil) {self.currentString = [[NSMutableString alloc] initWithString:@ ""] } if ([self.currentTag isEqualToString:@ "name"] | | [self.currentTag isEqualToString:@ "id"] | | [self.currentTag isEqualToString:@ "url"] | | [self.currentTag isEqualToString:@ "info"]) {[self.currentString appendString:string] }}-(void) parser: (NSXMLParser *) parser didEndElement: (NSString *) elementName namespaceURI: (NSString *) namespaceURI qualifiedName: (NSString *) qName {if ([elementName isEqualToString:@ "Shop"]) {[self.tmpList addObject:self.tmpShop]; [self.tmpShop release];} if ([elementName isEqualToString:@ "name"]) {self.tmpShop.name = [self.currentString copy] [self.currentString setString:@ ""]} else if ([elementName isEqualToString:@ "id"]) {self.tmpShop._id = [self.currentString copy]; [self.currentString setString:@ ""];} else if ([elementName isEqualToString:@ "url"]) {self.tmpShop.url = [self.currentString copy]; [self.currentString setString:@ ""] } else if ([elementName isEqualToString:@ "info"]) {/ / self.tmpShop.info = [self.currentString copy]; / / [self.currentString setString:@ "];}}-(void) parserDidStartDocument: (NSXMLParser *) parser {NSLog (@" start parsing xml file ");}-(void) parserDidEndDocument: (NSXMLParser *) parser {[self.tableView reloadData] NSLog (@ "parsing xml file completed");}

The structure of Shopdata is:

@ property (nonatomic,retain) NSString * name;@property (nonatomic,retain) NSString * url;@property (nonatomic,retain) NSString * _ id;@property (nonatomic,retain) NSString * info;@property (nonatomic,retain) UIImage * appIcon; Thank you for reading! This is the end of this article on "how IOS parses XML files". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report