【问题标题】:xml parsing of img src from the XML response从 XML 响应中解析 img src 的 xml
【发布时间】:2013-05-10 22:45:01
【问题描述】:

我有一个 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 库。

提前致谢!!

【问题讨论】:

    标签: ios objective-c xml parsing asihttprequest


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      相关资源
      最近更新 更多