使用RXMLElement工具類解析XML
阿新 • • 發佈:2018-12-31
現在來寫一篇我最近使用過的一種XML解析的方法的文章。XML解析有多種多樣的方法,包括蘋果自帶的方法和各種工具類,今天我只介紹用RXMLElement的工具類來解析XML。
使用步驟:
1.首先把XML解析類匯入工程;
2.然後在加入框架libxml2.dylib;
3.最後把Header search Paths的路徑設定一下:/usr/include/libxml2
最後上程式碼:
//MARK:解析xml - (void)analysisXmlContents{ // NSString *xmlFileString = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"xml"]; // // NSData *xmlData = [NSData dataWithContentsOfFile:xmlFileString]; // // NSString*conte=[NSString stringWithContentsOfFile:xmlFileString encoding:NSUTF8StringEncoding error:nil]; // stringcontents=conte; //獲取儲存的檔案路徑 NSString*filepath=[[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"app.xml"]; //獲取檔案內容 NSString*conte=[NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil]; stringcontents=conte; if ([self isBlankString:stringcontents]) { NSString *xmlFileString = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"xml"]; NSString*conte=[NSString stringWithContentsOfFile:xmlFileString encoding:NSUTF8StringEncoding error:nil]; stringcontents=conte; } if([self isBlankString:stringcontents]) { return; }else { } //解析檔案內容 NSLog(@"path=%@",filepath); RXMLElement*rootxml=[RXMLElement elementFromXMLString:stringcontents encoding:NSUTF8StringEncoding]; self.allArray=[NSMutableArray arrayWithCapacity:0]; self.allArray2=[NSMutableArray arrayWithCapacity:0]; self.allArray3=[NSMutableArray arrayWithCapacity:0]; self.yijiArray=[NSMutableArray arrayWithCapacity:0]; self.yijiArray2=[NSMutableArray arrayWithCapacity:0]; self.yijiArray3=[NSMutableArray arrayWithCapacity:0]; self.erjiArray=[NSMutableArray arrayWithCapacity:0]; self.sanjiArray=[NSMutableArray arrayWithCapacity:0]; self.erjiarray=[NSMutableArray arrayWithCapacity:0]; self.sanjiarray=[NSMutableArray arrayWithCapacity:0]; //解析資料 [rootxml iterate:@"Content" usingBlock:^(RXMLElement*xmlContent){ NSString*contents_ele=[xmlContent attribute:@"Name"]; if([contents_ele isEqualToString:@"OnArea11Light"]){ kaizhaomingAction=[xmlContent attribute:@"Action"]; //取得開燈Action }; //取得關燈的 Action if([contents_ele isEqualToString:@"OffArea11Light"]){ guanzhaomingAction=[xmlContent attribute:@"Action"]; } //取得開投影的Action if([contents_ele isEqualToString:@"OnArea11Monitor"]){ kaipingmuAction=[xmlContent attribute:@"Action"]; //取得開燈Action }; //取得關投影的 Action if([contents_ele isEqualToString:@"OffArea11Monitor"]){ guanpingmuAction=[xmlContent attribute:@"Action"]; } //取得開投影的Action if([contents_ele isEqualToString:@"OnArea11TouchScreen"]){ kaichumopingAction=[xmlContent attribute:@"Action"]; //取得開燈Action }; //取得關投影的 Action if([contents_ele isEqualToString:@"OffArea11TouchScreen"]){ guanchumopingAction=[xmlContent attribute:@"Action"]; } //取得開投影的Action if([contents_ele isEqualToString:@"OnArea11LedTitle"]){ kailedtitleAction=[xmlContent attribute:@"Action"]; //取得開燈Action }; //取得關投影的 Action if([contents_ele isEqualToString:@"OffArea11LedTitle"]){ guanledtitleAction=[xmlContent attribute:@"Action"]; } //取得開投影的Action if([contents_ele isEqualToString:@"OnArea11PC"]){ kaipcAction=[xmlContent attribute:@"Action"]; //取得開燈Action }; //取得關投影的 Action if([contents_ele isEqualToString:@"OffArea11PC"]){ guanpcAction=[xmlContent attribute:@"Action"]; } if([contents_ele isEqualToString:@"OnArea11ElectricDoor1"]){ kaidiandongmen1Action=[xmlContent attribute:@"Action"]; //取得開燈Action }; //取得關投影的 Action if([contents_ele isEqualToString:@"OffArea11ElectricDoor1"]){ guandiandongmen1Action=[xmlContent attribute:@"Action"]; } //取得開投影的Action if([contents_ele isEqualToString:@"OnArea11ElectricDoor2"]){ kaidiandongmen2Action=[xmlContent attribute:@"Action"]; //取得開燈Action }; //取得關投影的 Action if([contents_ele isEqualToString:@"OffArea11ElectricDoor2"]){ guandiandongmen2Action=[xmlContent attribute:@"Action"]; } if ([contents_ele rangeOfString:@"Area11Point1"].length>0) { NSString*title=[xmlContent attribute:@"Title"]; NSString*urlaction=[xmlContent attribute:@"Action"]; NSString*name=[xmlContent attribute:@"Name"]; NSDictionary *Dict=[NSDictionary dictionaryWithObjectsAndKeys:name,@"Name",title, @"Title",urlaction, @"Action",nil]; [self.allArray addObject:Dict]; } if ([contents_ele rangeOfString:@"Area11Point2"].length>0) { NSString*title=[xmlContent attribute:@"Title"]; NSString*urlaction=[xmlContent attribute:@"Action"]; NSString*name=[xmlContent attribute:@"Name"]; NSDictionary *Dict=[NSDictionary dictionaryWithObjectsAndKeys:name,@"Name",title, @"Title",urlaction, @"Action",nil]; [self.allArray2 addObject:Dict]; } if ([contents_ele rangeOfString:@"Area11Point3"].length>0) { NSString*title=[xmlContent attribute:@"Title"]; NSString*urlaction=[xmlContent attribute:@"Action"]; NSString*name=[xmlContent attribute:@"Name"]; NSDictionary *Dict=[NSDictionary dictionaryWithObjectsAndKeys:name,@"Name",title, @"Title",urlaction, @"Action",nil]; [self.allArray3 addObject:Dict]; } if ([contents_ele rangeOfString:@"Area11Point1Content"].length>0) { NSMutableString *mstr = [[NSMutableString alloc] initWithString:contents_ele]; [mstr deleteCharactersInRange:NSMakeRange(0, 19)]; if (mstr.length==1) { NSString*title=[xmlContent attribute:@"Title"]; NSString*urlaction=[xmlContent attribute:@"Action"]; NSString*name=[xmlContent attribute:@"Name"]; NSDictionary *Dict=[NSDictionary dictionaryWithObjectsAndKeys:name,@"Name",title, @"Title",urlaction, @"Action",nil]; [self.yijiArray addObject:Dict]; } } if ([contents_ele rangeOfString:@"Area11Point2Content"].length>0) { NSMutableString *mstr = [[NSMutableString alloc] initWithString:contents_ele]; [mstr deleteCharactersInRange:NSMakeRange(0, 19)]; if (mstr.length==1) { NSString*title=[xmlContent attribute:@"Title"]; NSString*urlaction=[xmlContent attribute:@"Action"]; NSString*name=[xmlContent attribute:@"Name"]; NSDictionary *Dict=[NSDictionary dictionaryWithObjectsAndKeys:name,@"Name",title, @"Title",urlaction, @"Action",nil]; [self.yijiArray2 addObject:Dict]; } } if ([contents_ele rangeOfString:@"Area11Point3Content"].length>0) { NSMutableString *mstr = [[NSMutableString alloc] initWithString:contents_ele]; [mstr deleteCharactersInRange:NSMakeRange(0, 19)]; if (mstr.length==1) { NSString*title=[xmlContent attribute:@"Title"]; NSString*urlaction=[xmlContent attribute:@"Action"]; NSString*name=[xmlContent attribute:@"Name"]; NSDictionary *Dict=[NSDictionary dictionaryWithObjectsAndKeys:name,@"Name",title, @"Title",urlaction, @"Action",nil]; [self.yijiArray3 addObject:Dict]; } } }]; }