iphone:使用 UIWebView 获取 xml?

发布于 2024-09-28 10:28:55 字数 2144 浏览 2 评论 0 原文

我有以下网址 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/&quot;DUADR347eCp7ImA9Wx5UFkg.&quot;'><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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扬花落满肩 2024-10-05 10:28:55

数据正在转义,您需要取消转义。看一下:

- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

“顺便说一句,对于那些想知道为什么我要费心这样做的人 - 我认为这种方法可以更快地获取数据。”

不要过早优化。

The data is being escaped, you need to unescape it. Take a look at:

- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

"BTW to those who are wondering why I'm even bothering to do this - I think that this method gets the data quicker."

Don't prematurely optimize.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文