Objective-C - NSXML阅读新行,我不会

时间:2012-04-19 09:20:11

标签: objective-c ios xml nsxmlparser

我的问题非常简单。我正在解析XML,并且使用NSXML解析过程非常好。我把所有声音放在一些数组中,现在我用它组成一个字符串。我的问题是,如果我以这种方式编写XML ...

<main>
  <introductions>
    <text>Text 01</text> 
    <text>Text 02</text>
  </introductions>
</main>

...我将2个字符串附加到我获得的故事板上的标签中:

Text 01
Text 02

我试图找出新线路的来源,所以我尝试了一些东西,我发现这个XML ......

<main>
  <introductions>
    <text>Text 01</text> <text>Text 02</text>
  </introductions>
</main>

...生产......

Text 01 Text 02

为什么这样?为什么NSXML正在阅读标签之外的内容?我怎么能避免这个?

如果它有帮助,那就是我如何初始化解析器:

 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];

这是代码的其余部分(对于我的真实XML):

- (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];
    } 
}

1 个答案:

答案 0 :(得分:1)

委托方法parser:foundCharacters:将提供所有字符数据,即您要查找的数据以及标记之间的所有空格。

当收到第一个结束</text>和第二个开放<text>之间的空白时,变量currentElement仍然包含"testo"。因此,即使解析器位于您感兴趣的标记之外,也要将空格(换行符)附加到currentName

为了解决这个问题,您需要更好地处理currentElement(例如在每个结束标记后将其设置为nil),或者您还需要另外检查堆栈深度。