iOS xml解析

來源:互聯網
上載者:User

標籤:

iOS xml 解析方法有兩種:SAX , DOM.

SAX 不用藉助第三方,在解析過程中逐行解析.(NSXMLParser,需要實現代理),具體步驟:

1.擷取檔案路徑.

2.建立 NSXMLParser 對象.

3.遵守協議,設定代理.

4.實現代理方法

  1>- (void)parserDidStartDocument:  ---- 開始解析

  2>- (void)parserDidEndDocument: ----解析結束

  3>- (void)parser:didStartElement:namespaceURI:qualifiedName:attributes: ----遇到開始標籤

  4>- (void)parser:didEndElement:namespaceURI:qualifiedName: ----遇到結束標籤

  5>- (void)parser: foundCharacters:  -----遇到值

 

詳細代碼:

 1 - (IBAction)xmlParserWithSAX:(id)sender { 2      3     //擷取檔案路徑 4     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"]; 5     //轉換為url 6     NSURL *url = [[NSURL alloc]initFileURLWithPath:filePath]; 7     //建立 NSXMLParser 對象 8     NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:url]; 9     parser.delegate = self;10     //開始解析11     [parser parse];12 }

代理方法實現:

 1 /** 2  *  開始解析 xml 3  */ 4 - (void)parserDidStartDocument:(NSXMLParser *)parser{ 5     NSLog(@"開始解析"); 6     self.studentArray = [[NSMutableArray alloc]init]; 7 } 8  9 10 /**11  *  結束解析 xml12  */13 - (void)parserDidEndDocument:(NSXMLParser *)parser{14     NSLog(@"解析結束");15     for (StudentModel *stu in self.studentArray) {16         NSLog(@"%@",stu);17     }18 }19 20 /**21  *  遇到開始標籤22  */23 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{24     NSLog(@"遇到開始節點:%@",elementName);25     if ([elementName isEqualToString:@"student"]) {26         self.studentModel = [[StudentModel alloc]init];27     }28 }29 30 /**31  *  遇到結束標籤32  */33 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{34     NSLog(@"遇到結束節點:%@",elementName);35    if ([elementName isEqualToString:@"name"]){36         self.studentModel.name = self.str;37     }else if ([elementName isEqualToString:@"age"]){38         self.studentModel.age = self.str;39     }else if ([elementName isEqualToString:@"gender"]){40         self.studentModel.gender = self.str;41     }else if ([elementName isEqualToString:@"student"]) {42         [self.studentArray addObject:self.studentModel];43     }44 }45 46 /**47  *  遇到的值48  */49 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{50     NSLog(@"%@",string);51     self.str = string;52 }

 

DOM 解析,先擷取全部檔案內容,找出階層,需要用到第三方(GDataXMLNode).具體步驟:

1.修改配置 Header Search Paths 加上:/usr/include/libxml2  

  Other Linker Flags  加上:-lxml2

2.擷取檔案路徑

3.擷取檔案內容

4.擷取 xml 結構

5.遍曆節點取值

詳細代碼:

 1 - (IBAction)xmlParserWithDOM:(id)sender { 2      3     //擷取檔案地址 4     NSString *filPath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"]; 5     NSError *error = nil; 6     //擷取檔案內容 7     NSString *contact = [NSString stringWithContentsOfFile:filPath encoding:NSUTF8StringEncoding error:&error]; 8     if (error == nil) { 9         NSLog(@"%@",contact);10     }else {11         NSLog(@"%@",error);12     }13     //第三方14     GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:contact options:0 error:nil];15     16     //擷取 xml 根節點17     GDataXMLElement *rootElement = [document rootElement];18     19     //擷取 根節點包含的節點20     NSArray *array = [rootElement elementsForName:@"student"];21     22     NSMutableArray *arr = [[NSMutableArray alloc]init];23     24     //遍曆節點,取值25     for (GDataXMLElement *ele in array) {26         StudentModel *model = [[StudentModel alloc]init];27         model.name = [[[ele elementsForName:@"name"] firstObject] stringValue];28         model.age = [[[ele elementsForName:@"age"] firstObject] stringValue];29         model.gender = [[[ele elementsForName:@"gender"] firstObject] stringValue];30         [arr addObject:model];31         32     }33     34     for (StudentModel *stu in arr) {35         NSLog(@"%@",stu);36     }37     38 }

 

iOS xml解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.