【问题标题】:nsxmlparser not solving 'nsxmlparser 没有解决 '
【发布时间】:2010-12-18 19:52:09
【问题描述】:

我正在使用 NSXMLParser 剖析一个 xml 包,我在包文本中收到 &apos。

我为 xmlParser 定义了以下内容:

[xmlParser setShouldResolveExternalEntities: YES];

以下方法永远不会被调用

- (void)parser:(NSXMLParser *)parser foundExternalEntityDeclarationWithName:(NSString *)entityName publicID:(NSString *)publicID systemID:(NSString *)systemID

解析器不考虑 &apos 之前字段中的文本。

我正在寻找如何解决这个问题,有什么想法吗???

提前致谢 亚历克斯

附上XML包部分:

<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:appwsdl"><SOAP-ENV:Body><ns1:getObjects2Response xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><return xsi:type="tns:objectsResult"><totalRecipes xsi:type="xsd:string">1574</totalObjects><Objects xsi:type="tns:Item"><id xsi:type="xsd:string">4311</id><name xsi:type="xsd:string"> item title 1 </name><procedure xsi:type="xsd:string">item procedure 11......

【问题讨论】:

    标签: iphone cocoa-touch xml-parsing


    【解决方案1】:

    在参考了这里的不同答案后,这就是我所做的。 当从 NSURLConnection 对象接收到数据时,我将 xml 中所有出现的 &amp;amp;apos; 替换为 "'"。然后我将该数据提供给解析器。

    所以我要做的是:

    NSData* parserData = [self resolveHTMLEntities: self.receivedData];
    NSXMLParser* parser = [[NSXMLaParser alloc] initwithData:parserData];
    

    这里是 resolveHTMLEntitites 方法:

    NSString *xmlCode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSMutableString *temp = [NSMutableString stringWithString:xmlCode];
    
    // Replace all the entities
    [temp replaceOccurrencesOfString:@"&amp;apos;" withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
    
    NSData *finalData = [temp dataUsingEncoding:NSUTF8StringEncoding];
    return finalData;
    

    问题是 &amp;amp;apos; 被转换为 &amp;amp;apos; 这就是我们需要替换该事件的原因。

    注意:以上代码块中没有进行内存管理。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      标准实体是&amp;lt;&amp;gt;&amp;amp;&amp;quot;&amp;apos; 是一个 html 实体引用。您的 XML 是指 XHTML 命名空间还是其他定义了 &amp;apos; 的命名空间?

      (顺便说一句,很高兴看到 XML 的一小段,包括标题。)

      【讨论】:

      • 我附加了 XML 部分,解析器对 &amp 也是如此,非常感谢您的帮助。
      • 我的 Mac 不在身边,所以目前无法运行测试,但您尝试过 foundInternalEntityDeclarationWithName 吗?
      • 是的,我尝试了所有与实体相关的代表,但没有人被调用 :( 非常感谢 Epsilon :)
      • 貌似three20使用resolveExternalEntityName填写要解析的实体列表:github.com/facebook/three20/blob/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 2018-07-29
      • 2015-12-26
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多