1. Note Two, one. Add/USR/INCLUDE/LIBXML2 two. Set GDATAXMLNODE.M to MRC
1 #import "GDataViewController.h"2 #import "GDataXMLNode.h"3 #import "Student.h"4 #import "Cartoon.h"5 @interfaceGdataviewcontroller ()6 //Array Properties7@property (nonatomic,strong) Nsmutablearray *Stuarray;8@property (nonatomic,strong) Nsmutablearray *Cartoonarray;9 @endTen One @implementationGdataviewcontroller A -- (void) Viewdidload { - [Super Viewdidload]; the //Do any additional setup after loading the view. - } - -- (void) didreceivememorywarning { + [Super didreceivememorywarning]; - //Dispose of any resources the can be recreated. + } A-(Ibaction) Didclickgdataxmlbutton: (ID) Sender { atSelf.stuarray =[Nsmutablearray array]; - //Get XMLData -NSData *xmldata = [NSData datawithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"Xml_stu"OfType:@"txt"]]; - //reads the entire XML, returns the type Gdataxmldocument document -Gdataxmldocument *xmldocment = [[Gdataxmldocument alloc] initwithdata:xmldata options:0Error:nil]; - //Get root node inGdataxmlelement *rootelement =xmldocment.rootelement; - //Get all child nodes under the root node toNSLog (@"%@", Rootelement.children); + for(Gdataxmlelement *studenteleinchRootelement.children) { - //Create student Objects theStudent *stu =[[Student alloc] init]; * //get the value of the student node under the subnode $ for(Gdataxmlelement *valueeleinchStudentele.children) {Panax Notoginseng //KVC Assignment Value - [Stu SetValue:valueEle.stringValue forKey:valueEle.name]; the + } A //Add to Array the [Self.stuarray Addobject:stu]; +NSLog (@"%@", Self.stuarray); - } $ } $-(Ibaction) Didclickcartoonbutton: (ID) Sender { - //array of Cartoon -Self.cartoonarray =[Nsmutablearray array]; the //Get XMLData -NSData *xmldata = [NSData datawithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"Cartoon"OfType:@"XML"]];Wuyi //Get Documents theGdataxmldocument *xmldocment = [[Gdataxmldocument alloc] initwithdata:xmldata options:0Error:nil]; - //Get root node WuGdataxmlelement *rootelment =xmldocment.rootelement; - for(Gdataxmlelement *resultsinchRootelment.children) { About $ for(Gdataxmlelement *albuminfoeleinchResults.children) { -Cartoon *cartoon =[[Cartoon alloc] init]; - for(Gdataxmlelement *valueeleinchAlbuminfoele.children) { - if([Valueele.name isequaltostring:@"name"] || [Valueele.name isequaltostring:@"desc"]) { A [Cartoon setValue:valueEle.stringValue ForKey:valueEle.name]; + } the - } $ [Self.cartoonarray Addobject:cartoon]; the the } the } the //NSLog (@ "%@", Self.cartoonarray); - for(Cartoon *carinchSelf.cartoonarray) { inNSLog (@"Name:%@, Introduction:%@", Car.name,car.desc); the } the}
Gdataxmlnode Parsing xml