Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C - NSXML reading new line where I won't
    text
    copied!<p>My question is pretty simple. I'm parsing an XML and the parsing procedure went pretty good with NSXML. I put all the voices in some arrays and now I'm composing a string with that. My problem is that if I write the XML in this way...</p> <pre><code>&lt;main&gt; &lt;introductions&gt; &lt;text&gt;Text 01&lt;/text&gt; &lt;text&gt;Text 02&lt;/text&gt; &lt;/introductions&gt; &lt;/main&gt; </code></pre> <p>... and I append the 2 strings in a label on the storyboard I obtain:</p> <pre><code>Text 01 Text 02 </code></pre> <p>I was trying to figure out where that new line came from, so I tried some things and I discovered that this XML...</p> <pre><code>&lt;main&gt; &lt;introductions&gt; &lt;text&gt;Text 01&lt;/text&gt; &lt;text&gt;Text 02&lt;/text&gt; &lt;/introductions&gt; &lt;/main&gt; </code></pre> <p>... produces...</p> <pre><code>Text 01 Text 02 </code></pre> <p>Why this? Why the NSXML is reading what's outside the tags? How can I avoid this?</p> <p>If it can helps, that's how I initialize the parser:</p> <pre><code> NSURL *xml = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"frasi" ofType:@"xml"]]; xmlParser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfURL:xml]]; [xmlParser setDelegate:self]; [xmlParser setShouldProcessNamespaces:NO]; [xmlParser setShouldReportNamespacePrefixes:NO]; [xmlParser setShouldResolveExternalEntities:NO]; [xmlParser parse]; </code></pre> <p>And this is the rest of the code (for my real XML):</p> <pre><code>- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ currentElement = [elementName copy]; if ([currentElement isEqualToString:@"introduzioni"] || [currentElement isEqualToString:@"soggetti"] || [currentElement isEqualToString:@"verbi"] || [currentElement isEqualToString:@"luoghi"]){ ++depth; //[self showCurrentDepth]; NSLog(@"Sezione %@", elementName); sezione = [[NSString alloc] init]; sezione = currentElement; } else if ([currentElement isEqualToString:@"testo"]){ currentName = [[NSMutableString alloc] init]; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ if ([elementName isEqualToString:@"introduzioni"] || [elementName isEqualToString:@"soggetti"] || [elementName isEqualToString:@"verbi"] || [elementName isEqualToString:@"luoghi"]){ --depth; //[self showCurrentDepth]; } else if ([elementName isEqualToString:@"testo"]){ if (depth == 1){ NSLog(@"Testo: %@", currentName); if([sezione isEqualToString:@"introduzioni"]) [arrayIntroduzioni addObject:currentName]; if([sezione isEqualToString:@"soggetti"]) [arraySoggetti addObject:currentName]; if([sezione isEqualToString:@"verbi"]) [arrayVerbi addObject:currentName]; if([sezione isEqualToString:@"luoghi"]) [arrayLuoghi addObject:currentName]; } } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ if ([currentElement isEqualToString:@"testo"]){ [currentName appendString:string]; } } </code></pre> <hr>
 

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