gpt4 book ai didi

html - 如何在 iOS 中分隔 xml 标记内的值

转载 作者:行者123 更新时间:2023-11-28 21:42:00 26 4
gpt4 key购买 nike

在调用 API 时,我收到以下格式的响应:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<response>
<status>success</status>
<ads>&lt;a href='http://52.7.130.201/oapi/clickAd;jsessionid=679E3CFF39C3D7792E7F6915318E3156.soma-i-915abc39'&gt;&lt;img src='http://52.7.130.201/oapi/getAd;jsessionid=679E3CFF39C3D7792E7F6915318E3156.soma-i-915abc39'/&gt;&lt;/a&gt;</ads>
</response>

我正在检索标签内的整个值并存储在一个字符串内。下面是它的代码:

 NSString *html = [xml.documentRoot getNamedChild:@"ads"].text;

现在我需要将值分别存储在“ads”标签的“href”和“img src”中。该怎么做?请帮助..

最佳答案

//as the data you have provided

NSString *html =@"&lt;a href='http://52.7.130.201/oapi/clickAd;jsessionid=679E3CFF39C3D7792E7F6915318E3156.soma-i-915abc39'&gt;&lt;img src='http://52.7.130.201/oapi/getAd;jsessionid=679E3CFF39C3D7792E7F6915318E3156.soma-i-915abc39'/&gt;&lt;/a&gt";



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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com