2013-08-01 41 views
1

我在解析XML內容並在ui中顯示時遇到問題。 XML有就是這個樣子通過tbxml解析xml後刪除特殊字符

<fullDescription>3.2GHz PowerPC CPU ATI GPU 512 MB 700 MHz GDDR3 RAM 1x Wireless Game Controller 3x USB 2.0 port XBOX Live ready 20GB HDD HD-AV-Kabel für High-Definition Output (720p, 1080i) inkl.</fullDescription> 

但是之後我分析它(直通TBXML)我得到的字符串作爲

3.2GHz PowerPC CPU  ATI GPU  512 MB 700 MHz GDDR3 RAM  1x Wireless Game Controller  3x USB 2.0 port  XBOX Live ready  20GB HDD  HD-AV-Kabel für High-Definition Output (720p, 1080i)  inkl. 

我曾嘗試已經提到的解決方案的數量元素的含量清理特殊字符,例如 HTML character decoding in Objective-C/Cocoa Touch 甚至修改的方法,包括「A」,雙空間似乎沒有任何工作..

我不能使用Github上NSString category for HTML因爲該代碼似乎不兼容ARC,當我嘗試在我的項目中使用它時,出現各種各樣的錯誤。

有人可以幫助我在正確的方向..拉我的頭髮在這一段時間:-(我想,必須有一個簡單的方法在一個通用的方法來做到這一點。

回答

0

剛檢查你的XML文件相同的是編碼,因爲它在你的XML頭表示。

0

你試試這個?

// NSString_stripHtml.h 
// Copyright 2011 Leigh McCulloch. Released under the MIT license. 

#import <Foundation/Foundation.h> 

@interface NSString (stripHtml) 
- (NSString*)stripHtml; 
@end 

// NSString_stripHtml.m 
// Copyright 2011 Leigh McCulloch. Released under the MIT license. 

#import "NSString_stripHtml.h" 

@interface NSString_stripHtml_XMLParsee : NSObject<NSXMLParserDelegate> { 
@private 
    NSMutableArray* strings; 
} 
- (NSString*)getCharsFound; 
@end 

@implementation NSString_stripHtml_XMLParsee 
- (id)init { 
    if((self = [super init])) { 
     strings = [[NSMutableArray alloc] init]; 
    } 
    return self; 
} 

- (void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string { 
    [strings addObject:string]; 
} 
- (NSString*)getCharsFound { 
    return [strings componentsJoinedByString:@""]; 
} 
@end 

@implementation NSString (stripHtml) 
- (NSString*)stripHtml { 
    // take this string obj and wrap it in a root element to ensure only a single root element exists 
    NSString* string = [NSString stringWithFormat:@"<root>%@</root>", self]; 

    // add the string to the xml parser 
    NSStringEncoding encoding = string.fastestEncoding; 
    NSData* data = [string dataUsingEncoding:encoding]; 
    NSXMLParser* parser = [[NSXMLParser alloc] initWithData:data]; 

    // parse the content keeping track of any chars found outside tags (this will be the stripped content) 
    NSString_stripHtml_XMLParsee* parsee = [[NSString_stripHtml_XMLParsee alloc] init]; 
    parser.delegate = parsee; 
    [parser parse]; 

    // log any errors encountered while parsing 
    //NSError * error = nil; 
    //if((error = [parser parserError])) { 
    // NSLog(@"This is a warning only. There was an error parsing the string to strip HTML. This error may be because the string did not contain valid XML, however the result will likely have been decoded correctly anyway.: %@", error); 
    //} 

    // any chars found while parsing are the stripped content 
    NSString* strippedString = [parsee getCharsFound]; 

    // get the raw text out of the parsee after parsing, and return it 
    return strippedString; 
} 
@end