我的问题很简单。我正在解析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];
    }
}

最佳答案

委托方法parser:foundCharacters:将传递所有字符数据,即您要查找的数据以及标签之间的所有空白。

当收到第一个闭合</text>和第二个闭合<text>之间的空格时,变量currentElement仍包含"testo"。因此,即使解析器不在您感兴趣的标记之外,也可以将空格(换行符)附加到currentName

为了修复它,您可能需要更好地处理currentElement(例如在每个结束标记之后将其设置为nil),或者您需要另外检查堆栈深度。

10-08 03:33