ios - NSXMLParser not working -


i trying parse xml data url. working, expected data isn't being logged.

here sample of xml data

<?xml version="1.0" encoding="utf-8"?> <result> <row>  <id>1</id>  <menu_id>1</menu_id>  <group_id>1</group_id>  <name>merchandise</name>  <image>bag of beans.jpg</image> </row>  <row>  <id>4</id>  <menu_id>1</menu_id>  <group_id>1</group_id>  <name>misc</name>  <image>tea.jpg</image> </row> 

and here code:

#pragma mark - nsxmlparserdelegate - (void)parser:(nsxmlparser *)parser didstartelement:(nsstring *)elementname namespaceuri:(nsstring*)namespaceuri qualifiedname:(nsstring *)qname attributes:(nsdictionary *)attributedict {     // if have more complex object init here     // since i'm storing string won't     if ([elementname isequaltostring:@"name"]) {    // nslog(@"didstartelement - %@", elementname);     }     } - (void)parser:(nsxmlparser *)parser foundcharacters:(nsstring *)string {     if (!self->currentstringvalue) {     self->currentstringvalue = [[nsmutablestring alloc] init];    // nslog(@"didstartelement - %@", string);      }       [self->currentstringvalue appendstring:string]; }  - (void)parser:(nsxmlparser *)parser didendelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri     qualifiedname:(nsstring *)qname {          nsstring *currentstringvalue = [self->currentstringvalue                                 stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]];     self->currentstringvalue = nil;      if ([elementname isequaltostring:@"name"]) {             self->ourexpectedelementvalue = currentstringvalue;             nslog(@"%@",currentstringvalue);         }     } 

as can see in xml data, there more 1 'name' element , 3rd last line reads

nslog(@"%@",currentstringvalue);  

is meant log values within <name> tags, instead logs this:

2013-07-31 19:02:56.394 touch2go[1775:11303] merchandise 

as turned out in discussion, xml data did not have single top-level element required xml specification, looked this:

<row>      ... </row> <row>      ... </row> <row>      ... </row> 

the parser stops parsing after first complete "row" tag (and returns error condition). if data enclosed in single top-level element

<result>     <row>         ...    </row>    <row>         ...    </row>    <row>         ...    </row> </result> 

then elements parsed intended.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -