IOS SDK comes with the Nsxml framework is based on the SAX mode parsing, is event-driven, the main parsing work is done in nsxmlparsedelegate, the parsing process encountered start tags, end tags, document start, When the document ends and the string is triggered, the method in delegate is resolved, which is a cumbersome way to use it. In the actual work, we can choose a more simple and easy-to-use third-party framework Tbxml, which is: Https://github.com/71squared/tbxml
After the download is complete, we only need to add the Tbxml-headers and Tbxml-code folders to the project and add a macro definition to the project header file xxx-prefix.pch: #define Arc_enabled
Since Tbxml relies on the Libz.dylib library, it is also necessary to add this library to the framework of the project, add it, and compile the test run to see if there is an error, yes.
For example, we will parse the following XML file:
<?XML version= "1.0" encoding= "UTF-8"?><Notes> <NoteID= "1"> <CDate>2012-12-21</CDate> <Content>8 o'clock in the morning to the company</Content> <UserID>Tony</UserID> </Note> <NoteID= "2"> <CDate>2012-12-22</CDate> <Content>Release IOSBook1</Content> <UserID>Tony</UserID> </Note> <NoteID= "3"> <CDate>2012-12-23</CDate> <Content>Release IOSBOOK2</Content> <UserID>Tony</UserID> </Note> <NoteID= "4"> <CDate>2012-12-24</CDate> <Content>Release IOSBOOK3</Content> <UserID>Tony</UserID> </Note> <NoteID= "5"> <CDate>2012-12-25</CDate> <Content>Launch of the 2016 Olympics app iphone version</Content> <UserID>Tony</UserID> </Note> <NoteID= "6"> <CDate>2012-12-26</CDate> <Content>Launch of the 2016 Olympics app ipad version</Content> <UserID>Tony</UserID> </Note></Notes>
Create a Notestbxmlparser class to parse the XML document, implementing the following code:
// NotesTBXMLParser.h#import"TBXML.h" @interface Notestbxmlparser:nsobject// parsed data inside is dictionary type @property (strong,nonatomic) Nsmutablearray * notes; // Start parsing -(void) start; @end
#import "NotesTBXMLParser.h"@implementationNotestbxmlparser//Start parsing-(void) start{_notes= [NsmutablearrayNew]; TBXML* Tbxml = [[Tbxml alloc] Initwithxmlfile:@"Notes.xml"Error:nil]; Constructing a Tbxml object from a file
Tbxmlelement* Root =tbxml.rootxmlelement;//getting the root element object of a document//if root element is valid if(Root) {tbxmlelement* Noteelement = [TBXML childelementnamed:@"Note"Parentelement:root]; while(Noteelement! =Nil) {Nsmutabledictionary*dict = [NsmutabledictionaryNew];
Tbxmlelement*cdateelement = [TBXML childelementnamed:@"CDate"Parentelement:noteelement]; if(Cdateelement! =Nil) {NSString*cdate =[TBXML textforelement:cdateelement]; [Dict setvalue:cdate Forkey:@"CDate"]; }
Tbxmlelement*contentelement = [TBXML childelementnamed:@"Content"Parentelement:noteelement]; if(ContentElement! =Nil) {NSString*content =[TBXML textforelement:contentelement]; [Dict setvalue:content Forkey:@"Content"]; } tbxmlelement*useridelement = [TBXML childelementnamed:@"UserID"Parentelement:noteelement]; if(Useridelement! =Nil) {NSString*userid =[TBXML textforelement:useridelement]; [Dict setvalue:userid Forkey:@"UserID"]; } //Get id attributeNSString *_id = [TBXML valueofattributenamed:@"ID"forelement:noteelement Error:nil]; [Dict setvalue:_id Forkey:@"ID"]; [_notes addobject:dict]; Noteelement= [TBXML nextsiblingnamed:@"Note"Searchfromelement:noteelement]; }} NSLog (@"Parse Complete ..."); [[Nsnotificationcenter Defaultcenter] Postnotificationname:@"reloadviewnotification" Object: Self.notes Userinfo:nil]; Self.notes=Nil; }
@end