Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse XML to iPhone correctly
    text
    copied!<p>I have an XML file, but do not know what I have done wrong as the app crashed, I do not know how to solve it. Can anyone help giving me some comments.</p> <pre><code>&lt;list&gt; &lt;section name="Texts"&gt; &lt;item&gt; &lt;postID&gt;26&lt;/postID&gt; &lt;title&gt;A sample quiz&lt;/title&gt; &lt;url&gt;http://yahoo.com&lt;/url&gt; &lt;score&gt;20&lt;/score&gt; &lt;/item&gt; &lt;item&gt; &lt;postID&gt;230&lt;/postID&gt; &lt;title&gt;A sample quiz&lt;/title&gt; &lt;url&gt;http://www.yahoo.com&lt;/url&gt; &lt;score&gt;20&lt;/score&gt; &lt;/item&gt; &lt;/section&gt; &lt;section name="Images"&gt; &lt;item&gt; &lt;postID&gt;27&lt;/postID&gt; &lt;title&gt;A sample image quiz&lt;/title&gt; &lt;url&gt;www.google.com&lt;/url&gt; &lt;score&gt;10&lt;/score&gt; &lt;/item&gt; &lt;item&gt; &lt;postID&gt;27&lt;/postID&gt; &lt;title&gt;A sample image quiz&lt;/title&gt; &lt;url&gt;www.google.com&lt;/url&gt; &lt;score&gt;10&lt;/score&gt; &lt;/item&gt; &lt;/section&gt; &lt;/list&gt; </code></pre> <p>Code I have used:</p> <pre><code>-(allChallengeParser *)initXMLParser { self = [super init]; appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.allChallengeArray = [[NSMutableArray alloc]init]; xmlChallengeRoot =[[NSMutableArray alloc]init]; return self; } -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { //Implement method called NSXMLParser when it hits the start of an element: if([elementName isEqualToString:@"list"]) { NSLog(@"list root element found – create a new instance of challenge OBj..."); allChallengeObj = [[allChallenge alloc] init]; //We do not have any attributes in the user elements, but if // if have can extract them here: // user.att = [[attributeDict objectForKey:@"&lt;att name&gt;"] ...]; } else if([elementName isEqualToString:@"section"]) { allChallengeObj = [[allChallenge alloc] init]; allChallengeObj.section = [attributeDict valueForKey:@"name"]; NSLog(@"allChallengeObj.section %@", allChallengeObj.section); } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if(!currentElementValue) // init the ad hoc string with the value currentElementValue = [[NSMutableString alloc] initWithString:string]; else // append value to the ad hoc string [currentElementValue appendString:string]; NSLog(@"Processing Value for: %@", currentElementValue); } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:@"list"]) { return; } if ([elementName isEqualToString:@"section"]) { // We are done with user entry – add the parsed user // object to our user array [xmlChallengeRoot addObject:allChallengeObj]; // release user object allChallengeObj = nil; } else { // The parser hit one of the element values. // This syntax is possible because User object // property names match the XML user element names NSLog(@"elementName %@",elementName); NSLog(@"current Element Value %@",currentElementValue); [allChallengeObj setValue:currentElementValue forKey:elementName]; } currentElementValue = nil; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload