【问题标题】:going to fetch a value from xml response when going to parse but i am fail to get value解析时要从 xml 响应中获取值,但我无法获取值
【发布时间】:2012-01-20 09:17:38
【问题描述】:

以下代码是来自服务器的响应:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><SendCardDetailsResponse xmlns="http://tempuri.org/"><SendCardDetailsResult><ROOT xmlns=""><DocumentElement><res-auth><auth-data count='1' ><**attribute name='OTP1'** length='6' type='N' label='OTP' prompt='Please enter OTP as send to your mobile.'/></auth-data></res-auth></DocumentElement></ROOT></SendCardDetailsResult></SendCardDetailsResponse></soap:Body></soap:Envelope>

我必须获取属性名称“OTP1” 如何获取属性名称的值 任何帮助都将是可观的

【问题讨论】:

  • 我猜这不是 XML。检查下面
  • w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder. xsd"> John SmithOla Nordmann
    Langgt 23
    4000 Stavanger挪威
    帝国滑稽剧 特别版110.90隐藏你的心 19.90
  • 通过使用nsxmlparser类和解析委托方法

标签: iphone xcode xml-parsing objective-c-2.0


【解决方案1】:

我会这样使用:

-(NSString*) getAttribute{
    NSString *attribute = @"";
    NSString *xmlString = @"";//put the XML in this NSString
    int endOfSrc = [xmlString rangeOfString:@"\"><attribute name='"].location+[xmlString rangeOfString:@"\"><attribute name='"].length;
    int borderPos = [xmlString rangeOfString:@"' length='"].location;
    if(borderPos != NSNotFound){
        NSRange range;
        range.location = endOfSrc;
        range.length = borderPos - endOfSrc;
        attribute = [xmlString substringWithRange:range];
        return attribute;
    }
    return @" ";
}

这里没什么难的,如果出现关于代码的一些问题,只需查看我在此处使用的函数的文档即可。 也许有更有效的方法,不知道。它对我来说就像一个魅力。

【讨论】:

  • @dumoko..thanx 您的回复我正在使用此代码,但我对 rangeOfString 感到困惑,我必须给出 range 的值吗?你能解释一下如何使用这个方法吗......
  • rangeofString 为您提供 NSRange 值,其中包含 2 个字段:\">
猜你喜欢
  • 2012-06-19
  • 1970-01-01
  • 2017-02-16
  • 2016-01-03
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
相关资源
最近更新 更多