【问题标题】:removing special characters after parsing xml thru tbxml通过 tbxml 解析 xml 后删除特殊字符
【发布时间】:2013-08-01 21:35:35
【问题描述】:

我在解析 XML 内容并将其显示在 ui 中时遇到问题。 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 甚至修改了包含 "Â" 的方法,双空格似乎没有用..

我无法使用 Github NSString category for HTML,因为该代码似乎与 ARC 不兼容,并且当我尝试在我的项目中使用它时遇到各种错误。

有人可以帮助我朝正确的方向前进.. 把我的头发拉出来一会儿:-(..我认为必须有一种简单的方法以通用的方式做到这一点。

【问题讨论】:

    标签: html ios objective-c xml cocoa-touch


    【解决方案1】:

    只需检查 xml 文件的编码是否与 xml 标头中指示的相同。

    【讨论】:

      【解决方案2】:

      你试过了吗?

      //  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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-25
        相关资源
        最近更新 更多