2009-11-20 34 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;這就是爲什麼我們需要更換髮生。

注意:在上述代碼塊中沒有執行內存管理。

希望這會有所幫助。