NSXMLParser遇到特殊字符后停止解析 [英] NSXMLParser stops parsing after encountering special character

查看:63
本文介绍了NSXMLParser遇到特殊字符后停止解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Google Weather API中读取XML文件,并使用NSXMLParser对其进行解析.有问题的城市是巴黎.这是我得到的一个简短的xml输出

I am reading a XML file from google weather api and parsing it using NSXMLParser. The city in question is Paris. Here is a brief xml output I get

           <?xml version="1.0"?>
    <xml_api_reply version="1">
    <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information>
    <city data="Paris, Île-de-France"/>
    <postal_code data="Paris"/>
    <latitude_e6 data=""/>
    <longitude_e6 data=""/> 
...
...

现在我用来解析该xml的代码是

Now the code I used to pares this xml is

NSString *address = @"http://www.google.com/ig/api?weather=Paris";
    NSURL *URL = [NSURL URLWithString:address];

NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
    [parser setDelegate:self];
    [parser parse];
...

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

    NSLog(@"XML Parser 1 ... elementName ... %@", elementName);

}

这是我从上述xml中获得的输出

This is output that I get for the above xml

XML Parser 1 ... elementName ... xml_api_reply
XML Parser 1 ... elementName ... weather
XML Parser 1 ... elementName ... forecast_information

问题在于它会解析所有标签,直到到达城市数据"为止,因为巴黎(法兰西岛)的名称​​中包含一个非ASCII字符,然后它就停止了.之后,它不会像postal_code一样处理标签.纬度,经度等.

The problem is that it parses all the tags till it reaches "city data" since there is a non-ascii character in the name Paris, Île-de-France and then it just stops. It doesn't process tags afterwards like postal_code. latitude, longitude etc.

所以我的问题是,有什么办法可以从返回的URL XML字符串中删除所有非ASCII字符?

So my question is, is there a way I can remove all non-ascii characters from the returned URL XML string?

推荐答案

好.我已经解决了这个问题.这就是我的工作方式.

Ok. I have solved this problem. This is how I got it to work.

首先,我要从URL中获取带有特殊字符的XML.然后,我从XML字符串中去除所有特殊字符.然后,我将字符串转换为NSdata,然后将该nsdata对象传递给我的NSXMLParser.由于它不再包含特殊字符,因此NSXMLParser很高兴.

First I do is get the XML from the URL with special characters. Then I strip out all the special characters from the XML string. Then I convert the string to NSdata and then pass that nsdata object to my NSXMLParser. Since it has no more special characters NSXMLParser is happy.

这是将来可能遇到的任何人的代码.非常感谢对这篇文章有贡献的每个人!

Here's the code for anyone who may run across in future. Big thank you to everyone who contributed to this post!

NSString *address = @"http://www.google.com/ig/api?weather=Paris";
    NSURL *URL = [NSURL URLWithString:address];
    NSError *error;    
    NSString *XML = [NSString stringWithContentsOfURL:URL encoding:NSASCIIStringEncoding error:&error];

    //REMOVE ALL NON-ASCII CHARACTERS
         NSMutableString *asciiCharacters = [NSMutableString string];
         for (NSInteger i = 32; i < 127; i++)  
         {
         [asciiCharacters appendFormat:@"%c", i];
         }

         NSCharacterSet *nonAsciiCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:asciiCharacters] invertedSet];

         XML = [[XML componentsSeparatedByCharactersInSet:nonAsciiCharacterSet] componentsJoinedByString:@""];

    NSData *data = [XML dataUsingEncoding:NSUTF8StringEncoding];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];
    [parser parse];

NSXMLParser是一个可怕的工具.我已经在所有应用程序中成功使用了RaptureXML.它超级易于使用,避免了所有非ASCII字符的废话. https://github.com/ZaBlanc/RaptureXML

NSXMLParser is a horrible tool. I have successfully used RaptureXML in all my apps. Its super easy to use and avoids all this non-sense of non-ascii characters. https://github.com/ZaBlanc/RaptureXML

这篇关于NSXMLParser遇到特殊字符后停止解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆