在调用 API 时,我收到以下格式的响应:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<response>
<status>success</status>
<ads><a href='http://52.7.130.201/oapi/clickAd;jsessionid=679E3CFF39C3D7792E7F6915318E3156.soma-i-915abc39'><img src='http://52.7.130.201/oapi/getAd;jsessionid=679E3CFF39C3D7792E7F6915318E3156.soma-i-915abc39'/></a></ads>
</response>
我正在检索标签内的整个值并存储在一个字符串内。下面是它的代码:
NSString *html = [xml.documentRoot getNamedChild:@"ads"].text;
现在我需要将值分别存储在“ads”标签的“href”和“img src”中。该怎么做?请帮助..
最佳答案
//as the data you have provided
NSString *html =@"<a href='http://52.7.130.201/oapi/clickAd;jsessionid=679E3CFF39C3D7792E7F6915318E3156.soma-i-915abc39'><img src='http://52.7.130.201/oapi/getAd;jsessionid=679E3CFF39C3D7792E7F6915318E3156.soma-i-915abc39'/></a>";
NSString *href;
NSString *img_src;
NSRange range = [html rangeOfString:@"href=\'"];
NSString *tempUrl=[html substringFromIndex:range.location+6];
NSRange range2 = [tempUrl rangeOfString:@"\'"];
href=[tempUrl substringWithRange:NSMakeRange(0, range2.location)];
range = [html rangeOfString:@"img src=\'"];
tempUrl=[html substringFromIndex:range.location+9];
range2 = [tempUrl rangeOfString:@"\'"];
img_src=[tempUrl substringWithRange:NSMakeRange(0, range2.location)];
NSLog(@"your href is %@ and your img_src is %@",href,img_src);
关于html - 如何在 iOS 中分隔 xml 标记内的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31669964/