【问题标题】:Filtering out BOM characters from an NSXMLDocument从 NSXMLDocument 中过滤掉 BOM 字符
【发布时间】:2012-10-26 21:22:27
【问题描述】:

XML 文件中某些元素的 stringValue 中包含 BOM 字符。 xml 文件被标记为 UTF-8 编码。

其中一些字符位于字符串的开头(正如我所读到的那样),但有些字符位于字符串的中间(可能是编写 xml 文件的人的格式错误的字符串?)。

我正在打开文件:

NSURL *furl = [NSURL fileURLWithPath:fileName];
if (!furl) {
    NSLog(@"Error: Can't open NML file '%@'.", fileName);

    return kNxADbReaderTTError;
}

NSError *err=nil;

NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:NSXMLNodeOptionsNone error:&err];

我以这种方式查询元素:

NSXMLElement *anElement;
NSString *name;
...
NSString *valueString = [[anElement attributeForName:name] stringValue];

我的问题是:

我是不是打开文件有误?文件格式不正确?我查询元素的字符串值是否错误?如何过滤掉这些字符?

【问题讨论】:

    标签: objective-c xml nsxmldocument nsxmlelement


    【解决方案1】:

    在修复另一个问题时,我发现了一种从 NSXMLDocument 源中过滤掉不需要的字符的相对干净的方法。将其粘贴在这里以防万一有人遇到类似问题:

    @implementation NSXMLDocument (FilterIllegalCharacters)
    
        - (NSXMLDocument *)initWithDataAndIgnoreIllegalCharacters:(NSData *)data illegalChars:(NSCharacterSet *)illegalChars error:(NSError **)error{
        // -- Then, read the resulting XML string.
        NSMutableString *str = [[NSMutableString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
        // -- Go through the XML, only caring about attribute value strings
        NSMutableArray *charactersToRemove = [NSMutableArray array];
        NSUInteger openQuotes = NSNotFound;
        for (NSUInteger pos = 0; pos < str.length; ++pos) {
            NSUInteger currentChar = [str characterAtIndex:pos];
    
            if (currentChar == '\"') {
                if (openQuotes == NSNotFound) {
                    openQuotes = pos;
                }
                else {
    
                    openQuotes = NSNotFound;
                }
            }
            else if (openQuotes != NSNotFound) {
                // -- If we find an illegal character, we make a note of its position.
                if ([illegalChars characterIsMember:currentChar]) {
                    [charactersToRemove addObject:[NSNumber numberWithLong:pos]];
                }
            }
        }
    
        if (charactersToRemove.count) {
            NSUInteger index = charactersToRemove.count;
    
            // -- If we have characters to fix, we work thru them backwards, in order to not mess up our saved positions by modifying the XML.
            do {
                --index;
    
                NSNumber *characterPos = charactersToRemove[index];
                [str replaceCharactersInRange:NSMakeRange(characterPos.longValue, 1) withString:@""];
            }
            while (index > 0);
    
            // -- Finally we update the data with our corrected version
            data = [str dataUsingEncoding:NSUTF8StringEncoding];
        }
    
        return [[NSXMLDocument alloc] initWithData:data options:NSXMLNodeOptionsNone 
    
        error:error];
    }
    
    @end
    

    你可以传递任何你想要的字符集。请注意,这会将读取 XML 文档的选项设置为无。您可能想根据自己的目的更改此设置。

    这只会过滤属性字符串的内容,这是我的格式错误的字符串的来源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      相关资源
      最近更新 更多