在XML中搜索字符串

时间:2012-03-21 10:13:32

标签: iphone ios5 nsstring httpresponse

我收到了XML格式的Web服务调用的响应。我想在XML响应中搜索status字符串,并根据其值继续进行。我怎样才能做到这一点?回复如下:

<META NAME="ColdFusionMXEdition" CONTENT="ColdFusion DevNet Edition - Not for Production Use."><?xml version="1.0" encoding="UTF-8"?>
<ParticipantService>
    <Response>
        <student_id>10082</student_id>
    </Response>
    <RequestStatus>
        <Code>1</Code>
        <Status>SUCCESS</Status>
        <Message>Valid Credentials.</Message>
    </RequestStatus>
</ParticipantService>

3 个答案:

答案 0 :(得分:2)

您可以使用NSXmlParser。如果您只检查状态,可以按如下方式进行字符串操作,

NSString *mystring = @"<Response><status>SUCCESS</status> </Response>";
NSString *neededString = nil;
NSScanner *scanner =[NSScanner scannerWithString:mystring];
[scanner scanUpToString:@"<status>" intoString:NULL];
[scanner scanString:@"<status>" intoString:NULL];
[scanner scanUpToString:@"</status>" intoString:&neededString];
NSLog(@"%@",neededString);
if ([neededString isEqualToString:@"SUCCESS"]){  //do what you like. }

答案 1 :(得分:1)

您是否可以在开发库中访问XPath?

如果是这样,你可以使用(例如);

if (myDoc.xPath('/RequestStatus[Status = "SUCCESS"]').size() > 0) {  }

答案 2 :(得分:1)

查看NSXMLParser并查看Event-Driven XML Programming Guide

中的“XML解析基础”一章