0

我有一个 xml 响应:

{
    text = "<img alt=\"Marco Bueno\" src=\"http://u.goal.com/136600/136687_thumb.jpg\" style=\"float: left;margin:0 10px 10px 10px;\" title=\"Marco Bueno\" /><p style=\"float:left;\">Herrera is currently on national team duty representing the U-23 side that has already made history at the Toulon Tournament, while Bueno won the U-17 World Cup in 2011</p>";
}

现在我想从中获取 img src(URL)。我怎么能得到那个?

我为此使用 asihttpRequest 库。

提前致谢!!

4

1 回答 1

1

iOS SDK 中没有objective-c xml 解析器,但你有libxml2 C 库。您可以使用现有的几个开源 Objective-c xml 解析器实现之一,例如:https ://github.com/robbiehanson/KissXML

一旦你在你的项目中包含了 KissXML,你就可以做这样的事情(假设你的响应是 JSON 并且 XML 只是你的 JSON 响应的值之一):

 NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

 NSString xmlString = [jsonObject objectForKey:@"text"];

 NSXMLDocument *xml = [[NSXMLDocument alloc] initWithXMLString:xmlString options:NSXMLDocumentTidyHTML error:nil];

 NSString *url = [[[[xml rootElement] nodesForXPath:@"body/img/@src" error:nil] lastObject] stringValue];

 NSLog(@"%@", url);
于 2013-05-16T07:16:52.503 回答