【问题标题】:iphone: Getting xml with UIWebView?iphone:使用 UIWebView 获取 xml?
【发布时间】:2011-04-28 13:31:21
【问题描述】:

我有以下网址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,

为什么字符会发生变化?以及如何防止它

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

【问题讨论】:

    标签: iphone xml encoding uiwebview nsxmlparser


    【解决方案1】:

    数据正在转义,您需要对其进行转义。看看:

    - (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

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

    不要过早优化。

    【讨论】:

    • 这对我不起作用(UTF8 和 ASCII 编码不起作用)。你确定不是只针对 URL 吗?
    猜你喜欢
    • 2023-03-18
    • 2012-05-05
    • 2011-06-08
    • 2011-08-20
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    相关资源
    最近更新 更多