简体   繁体   English

如何使用NSXMLParser读取URL(带有特殊字符)?

[英]How to read URLs (with special characters) with NSXMLParser?

I download an XML file that contains several URLs. 我下载了一个包含多个URL的XML文件。 It works fine, but when I enter a URL in the XML like this: 它工作正常,但是当我在XML中输入如下URL时:

http://eda-bea.es/pub/record_card_1.php?refpage=%252Fpub%252Fsearch_select.php&quicksearch=adra&page=1&rec=3 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: 据我了解,URL包含特殊字符,我寻求并给出了以下代码作为可能的解决方案,其中在解析之前将信息编码为UTF8:

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

解决方案是使用现场

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

Changed Answer 改变答案

This link http://eda-bea.es/pub/record_card_1.php?refpage=%252Fpub%252Fsearch_select.php&quicksearch=adra&page=1&rec=3 is not an XML document, it's an HTML document. 此链接http://eda-bea.es/pub/record_card_1.php?refpage=%252Fpub%252Fsearch_select.php&quicksearch=adra&page=1&rec=3不是XML文档,而是HTML文档。

The parser will fail on <link rel="stylesheet" type="text/css" href="images.php/hispep.css"> because here is no end link tag. 解析器将在<link rel="stylesheet" type="text/css" href="images.php/hispep.css">因为这里没有结束链接标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM