Im using NSXMLParser to dissect a xml package, I'm receiving &apos inside the package text.
I have the following defined for the xmlParser:
[xmlParser setShouldResolveExternalEntities: YES];
The following method is never called
- (void)parser:(NSXMLParser *)parser foundExternalEntityDeclarationWithName:(NSString *)entityName publicID:(NSString *)publicID systemID:(NSString *)systemID
The text in the field before the &apos is not considered by the parser.
Im searching how to solve this, any idea???
Thanks in advance Alex
XML package portion attached:
<?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......
Here is what I did, after referring a different answer from here.
I replaced all the occurrences of the '
in the xml with "'"
when the data is received from NSURLConnection object. Then I give that data to the parser.
So what I do is:
NSData* parserData = [self resolveHTMLEntities: self.receivedData];
NSXMLParser* parser = [[NSXMLaParser alloc] initwithData:parserData];
Here is the resolveHTMLEntitites method:
NSString *xmlCode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableString *temp = [NSMutableString stringWithString:xmlCode];
// Replace all the entities
[temp replaceOccurrencesOfString:@"&apos;" withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
NSData *finalData = [temp dataUsingEncoding:NSUTF8StringEncoding];
return finalData;
The catch is that '
gets converted to &apos;
thats why we need to replace that occurrence.
Note: No memory management is performed in the above block of code.
Hope this helps.
The standard entities are <
, >
, &
, and "
. '
is an html entity reference. Does your XML refer to the XHTML namespace or some other namespace that has '
defined?
(BTW, would be nice to see a small segment of the XML including the header.)
精彩评论