objective c - Convert TBXML parser to NSXML parser -


i developing app ios device in xcode6 (just updated xcode5) @ point user pushes button , tableview seen information nicely incorporated in each cell, information details of corresponding object, , object specified identifier numeric value when he/she pushed button.

so basically, using segue method capture numeric value entered in textfield user in previous view (secondviewcontroller.m) there view button seen, number it's label. user pushes button , tableview pops in, showing details of object.

the data (details info) retrieved xml url, works fine using project tbxml parser.

but tested app on real device (iphone5s) , time push button in order see tableview , object details, happens nothing, if button not there, @ least functionality, in simulator works wonderful.

my boss told me change code use nsxml parser instead of tbxml parser. i've seen tutorials , don't though.

can me translate block of tbxml-code nsxml-code please. btw "object" tree, , details specific information of tree, humidity, taxonomy, height, temperature, etc.

here link xml url: http://papvidadigital.com/risi/?nid=83 (get info object 83)

is simple xml.

and here code involving parsing of xml.

//xml   //loading xml file //create link     nsstring *buildingurl = [nsstring stringwithformat:@"http://papvidadigital.com/risi/?nid=%@", _passingvaluetotable];     nsurl *myurl = [nsurl urlwithstring:buildingurl];  //setting data nsdata *mydata = [nsdata datawithcontentsofurl:myurl];  tbxml *sourcexml = [[tbxml alloc] initwithxmldata:mydata error:nil];   //extract elements tbxmlelement *rootelement = sourcexml.rootxmlelement;  tbxmlelement *datoelement = [tbxml childelementnamed:@"dato" parentelement:rootelement];    //extract attributes  //extract element  //nid //tbxmlelement *nidelement = [tbxml childelementnamed:@"nid" parentelement:datoelement]; //nsstring *nidelementstring = [tbxml textforelement:nidelement]; //nslog(@"nid: %@\n", [nidelementstring lowercasestring]);  //taxonomia tbxmlelement *taxonomiaelement = [tbxml childelementnamed:@"taxonomia" parentelement:datoelement]; nsstring *taxonomiaelementstring = [tbxml textforelement:taxonomiaelement]; nslog(@"taxonomia: %@\n", [taxonomiaelementstring lowercasestring]);    //diametro tbxmlelement *diametroelement = [tbxml childelementnamed:@"diametro" parentelement:datoelement];  nsstring *diametroelementstring = [tbxml textforelement:diametroelement]; nsstring *diametroelementtext = [ nsstring stringwithformat:@"%@ cm", diametroelementstring];      nslog(@"diametro: %@\n", [diametroelementstring lowercasestring]);  //verificar y validar icono correspondiente nsstring *thumbimagediametro; nsinteger diametroelementnumber = [diametroelementstring integervalue]; if(diametroelementnumber >= 30){     thumbimagediametro = @"diametroalto.png"; }else if(diametroelementnumber >= 15 && diametroelementnumber < 30){     thumbimagediametro = @"diametromedio.png"; }else if(diametroelementnumber < 15){     thumbimagediametro = @"diametropequeño.png"; } 

sorry terms in spanish. parse xml, found each child of "dato" , save what's inside ">" , "<" string value can later put in object array have put data cells. can see if-else statements "diametroelementnumber", because corresponding thumbimagediametro (image in corresponding cell) change according "diametroelementnumber" value. (tree diameter size). used simple cast integer.

here small example of object array:

_description = @[taxonomiaelementstring,                  plantadoelementstring,                  diametroelementtext]; 

and images object array (images in each cell):

_images = @[@"taxonomia.png",             @"fechadeplantacion.png",             thumbimagediametro]; 

and object array of fixed titles each cell:

_title = @[@"taxonomía",            @"año de plantado",            @"diámetro"]; 

and how put data each cell:

//put data cells - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {        //for cells have tablecell identifier     static nsstring *cellidentifier = @"tablecell";     cell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath];      // configure cell...      long row = [indexpath row];     cell.titlelabel.text = _title[row];      cell.descriptionlabel.text = _description[row];     //put corresponding image     cell.thumbimage.image = [uiimage imagenamed:_images[row]];      [cell setbackgroundcolor:[uicolor whitecolor]];      return cell;  } 

and generic/default methods tableview:

//calculates , returns number of sections in tableview controller - (nsinteger)numberofselectionsintableview:(uitableview *)tableview {     return 1; //number of sections } //calculates , returns number of rows in section - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     return _title.count; } 

please can me change necessary in order stop using external tbxml.h & tbxml.m file parse with, , use instead nsxml parser same mentioned.

basically want have translated code this.

thank in advance

if need convert code nsxml parser, documentation nsxmlparser class , nsxmlparserdelegate protocol best start.

to break down basics, can follow these steps start:

  1. announce class (likely view controller?) conforms nsxmlparserdelegate protocol putting in class header.
  2. create instance variable (or better yet, property) of nsxmlparser. can pull down data right url so:

    self.treeparser = [[nsxmlparser alloc] initwithcontentsofurl:myurl];

  3. make sure set self delegate can catch delegate callbacks. , can handle additional features here too, such resolving external entities, etc.

    self.treeparser.delegate = self; self.treeparser.shouldresolveexternalentities = yes;

  4. handle nsxmlparserdelegate methods want, likely:

    • parser:didstartelement:...,
    • parser:didendelement:..., `
    • parser:foundcharacters:
    • parser:parseerroroccurred:

the delegate methods listed above of work. can create instance variables (or properties) parsed objects , store them when parsing completed. when constructing consistent objects such tree data, best create custom class. example xml, have tree class taxonomia , plantado etc. properties store data service. follow basic parsing pattern

  1. in didstartelement:, if element "dato", want alloc/init custom tree class , store instance or property, currenttree perhaps.
  2. in foundcharacters:, store incoming characters instance or property, currentstring perhaps.
- (void)parser:(nsxmlparser *)parser foundcharacters:(nsstring *)string {         if (!self.currentstring) {             // currentstring nsmutablestring typed property             self.currentstring = [[nsmutablestring alloc] initwithcapacity:50];         }         [self.currentstring appendstring:string];     } 
  1. in didendelement:, if element "taxonomia" or "plantado" or other field want store class, assign currentstring class member. use kvc (assuming class complies , matches xml fields) following:

    [self.currenttree setvalue:self.currentstring forkey:elementname];

this save trouble of making several repetitive statements

if ([elementname isequaltostring:@"taxonomia"]) {...} 

you'll want set currentstring nil here don't end creating 1 long mutable string! also, if in didendelement elementname "dato", know object has completed can add collection or other actions there.

you should, of course, implement parseerroroccurred: handle errors may hit.

those basics. can expand more features of nsxmlparser class , delegate protocols. tableview need minor adjustments work tree class objects, , worth it.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -