我有以下网址 http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?&start-index=1&max-results=15&v=2
我正在尝试将其加载到 UIWebView 中并且然后使用javascript获取其内容,并使用NSXMLParser解析它。
我的代码如下所示:
-(void)startDownloading{
NSString *urlStr = [NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?&start-index=%d&max-results=%d&v=2", range.location, range.length];
NSLog(urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[browser loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *theStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.firstChild.innerHTML"];
NSLog(theStr);
NSData *receivedData = [theStr dataUsingEncoding:NSUTF8StringEncoding];
}
问题是我收到的数据无法用 NSXMLParser 进行解析。文本看起来像这样:
tag:youtube.com,2008:standardfeed:us:recently_featured...
而如果我只使用常规的获取数据的方法(没有浏览器)我会得到:
<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:app='http://www.w3.org/2007/app' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007' gd:etag='W/"DUADR347eCp7ImA9Wx5UFkg."'><id>tag:youtube.com,
为什么角色会改变? 顺便说一句,对于那些想知道为什么我要费心这样做的人来说,我怎样才能阻止它
——我认为这种方法可以更快地获取数据。
I have the following url http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?&start-index=1&max-results=15&v=2
I am trying to load it in UIWebView and then use javascript to get its contents, and parse it with NSXMLParser.
My code looks like that:
-(void)startDownloading{
NSString *urlStr = [NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?&start-index=%d&max-results=%d&v=2", range.location, range.length];
NSLog(urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[browser loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *theStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.firstChild.innerHTML"];
NSLog(theStr);
NSData *receivedData = [theStr dataUsingEncoding:NSUTF8StringEncoding];
}
the problem is that the data that I receive can't be parsed with NSXMLParser. The text looks like that:
<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:app='http://www.w3.org/2007/app' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007' gd:etag='W/"DUADR347eCp7ImA9Wx5UFkg."'><id>tag:youtube.com,2008:standardfeed:us:recently_featured</id><updated>...
while if I had used just the regular approach of getting data (without browser) I would get:
<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:app='http://www.w3.org/2007/app' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007' gd:etag='W/"DUADR347eCp7ImA9Wx5UFkg."'><id>tag:youtube.com,
Why are the characters changing? And how can I prevent it
BTW to those who are wondering why I'm even bothering to do this - I think that this method gets the data quicker.
发布评论
评论(1)
数据正在转义,您需要取消转义。看一下:
- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
不要过早优化。
The data is being escaped, you need to unescape it. Take a look at:
- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Don't prematurely optimize.