NSXMLParser与\ n的奇怪行为

时间:2011-12-13 16:39:32

标签: ios xml nsxmlparser file-format

我正在开发一个解析XML文件的iOS 4应用程序。一段XML是:

<?xml version="1.0" encoding="utf-8" ?>
<cards>
  <card id ="0">
    <name lang="es">The Mad</name>
    <description lang="es">...</description>
  </card>
...
</cards>

我使用以下方法来解析<name lang="es">The Mad</name>

#pragma mark -
#pragma mark NSXMLParserDelegate methods

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName 
    attributes:(NSDictionary *)attributeDict
{
    NSLog(@"DidStartElement: %@", elementName);
    if ([elementName isEqualToString:@"card"])
    {
        currentCard = [[Card alloc] init];
        NSString* arcaneNumber = [attributeDict objectForKey:@"id"];
        currentCard.Number = arcaneNumber;

        return;
    }
    if ([elementName isEqualToString:@"name"])
    {
        if ([[attributeDict objectForKey:@"lang"] isEqualToString:userLanguage])
        {
            currentProperty = kNameProperty;
            return;
        }
    }
    if ([elementName isEqualToString:@"description"])
    {
        if ([[attributeDict objectForKey:@"lang"] isEqualToString:userLanguage])
        {
            currentProperty = kDescriptionProperty;
            return;
        }
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (!currentStringValue)
    {
        // currentStringValue is an NSMutableString instance variable
        currentStringValue = [[NSMutableString alloc] initWithCapacity:50];
    }
    [currentStringValue appendString:string];
}

- (void) parser:(NSXMLParser *)parser
  didEndElement:(NSString *)elementName
   namespaceURI:(NSString *)namespaceURI
  qualifiedName:(NSString *)qName
{
    NSLog(@"DidEndElement: %@", elementName);
    if ([elementName isEqualToString:@"card"])
    {
        [cards setObject:currentCard forKey:currentCard.Number];
        [currentCard release];
        currentCard = nil;

        return;
    }

    if (currentProperty == kNameProperty)
    {
        currentProperty = kNoneProperty;
        currentCard.Name = [currentStringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [currentStringValue release];
        currentStringValue = nil;

        return;
    }

    if (currentProperty == kDescriptionProperty)
    {
        currentProperty = kNoneProperty;
        currentCard.Description = currentStringValue;
        [currentStringValue release];
        currentStringValue = nil;

        return;
    }
}

解析之后,我得到了以下的currentStringValue:

\n\nThe Mad

如何避免这两个'\ n'?为什么我得到这两个'\ n'?

此xml文件来自Windows系统,我使用TextWrangler将其转换为Classic Mac格式。

3 个答案:

答案 0 :(得分:7)

还会调用foundCharacters委托方法,以便在元素之间使用空格。如果在<cards><card id=...之间有换行符,则会调用它。

我建议在currentStringValue的顶部清除didStartElement以丢弃在当前元素开头之前找到的任何字符,并确保只捕获当前元素内部的字符(不在之间) foundCharacters

didStartElement的顶部,添加:

[currentStringValue release];
currentStringValue = nil;


元素的值可能包含空格(因此在didEndElement中删除它们可能会导致保存与xml内容不匹配的值。)

答案 1 :(得分:0)

在end元素方法上,您可以尝试从当前文本字符串中自行删除它们。只需尝试在字符串上搜索它们并用@“”替换它们。我喜欢替换这些和不需要的空白条目。

答案 2 :(得分:0)

尝试一下对我有用,

NSString *data= [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

foundCharacters 方法中使用上面的代码行。

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
 {
   NSString *data= [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

  if(dString == nil){
   dString = [[NSMutableString alloc] initWithString:data];
 }else
  {
   [dString appendString:data];
  }
 }

希望这对某些人有帮助。