Search code examples
iosurlnsxmlparserencode

How to read URLs (with special characters) with NSXMLParser?


I download an XML file that contains several URLs. It works fine, but when I enter a URL in the XML like this:

http://eda-bea.es/pub/record_card_1.php?refpage=%252Fpub%252Fsearch_select.php&quicksearch=adra&page=1&rec=3

the parser stops.

From what I understand is that the URL contains special characters, I have sought and given as a possible solution the following code, where information is encoded to UTF8 before parsing:

NSString *dataString = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:error];
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];

[parser parse];

It does not work. Stop parsing in this method

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
                                 namespaceURI:(NSString *)namespaceURI 
                                qualifiedName:(NSString *)qName 
                                   attributes:(NSDictionary *)attributeDict

Any ideas?

Thanks.


Solution

  • the solution was using the field

    <![CDATA[http://eda-bea.es/pub/record_card_1.php?refpage=%252Fpub%252Fsearch_select.php&quicksearch=adra&page=1&rec=3]]>