2009-11-20 31 views
0

我使用NSXMLParser解析一个xml包,我在包文本中收到&。nsxmlparser解决不了'

我有以下的XMLPARSER定义:

[xmlParser setShouldResolveExternalEntities: YES]; 

下面的方法不会被调用

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

的&者之前在字段中的文本不会被解析器考虑。

我正在寻找如何解决这个,任何想法???

预先感谢附 亚历

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

回答

0

标准实体是&lt;&gt;&amp;,和&quot;&apos;是一个html实体引用。您的XML是否引用了XHTML名称空间或其他名称空间,该名称空间已定义了&apos;

(顺便说一句,会很高兴地看到XML包括头部的一小部分。)

+0

我接触了XML部分,解析器对&amp相同,非常感谢您的帮助。 – alex 2009-11-20 21:15:39

+0

我远离我的Mac,因此我现在无法运行测试,但是您是否尝试过'foundInternalEntityDeclarationWithName'? – 2009-11-20 22:56:36

+0

是的,我尝试了所有的实体相关的代表,没有人被称为:( 感谢很多的Epsilon :) – alex 2009-11-20 23:15:24

1

这是我做什么,从这里指不同的答案后。 当我从NSURLConnection对象收到数据时,我用"'"替换了xml中&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; 

美中不足的是,&apos;被转换到&amp;apos;这就是为什么我们需要更换发生。

注意:在上述代码块中没有执行内存管理。

希望这会有所帮助。