【问题标题】:how to parse xml in iPhone ,如何在 iPhone 中解析 xml,
【发布时间】:2013-03-29 06:26:28
【问题描述】:

我有包含 xml 响应的字符串,(asmx Web 服务) 我正在通过 APXML lib 解析 xml。 当我得到给我身体标签的根元素时,我不想要身体标签,我想获得根标签中的实际数据而不是子标签.. 请告诉我如何从中获取数据.. 谢谢.. 我的 xml 是

 <?xml version="1.0" encoding="UTF-8" ?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
 <loginFunctionResponse xmlns="http://tempuri.org/">
  <loginFunctionResult>
   <Root xmlns="">
    <child>
     <id>52</id>
     <name>mohsin</name>
   </child>
  </Root>
 </loginFunctionResult>
</loginFunctionResponse>
</soap:Body>
</soap:Envelope>

我的代码是..

NSString *resultString=[[NSString alloc] initWithBytes:[resultData bytes] length:resultData.length encoding:NSUTF8StringEncoding];
NSLog(@"result string: %@",resultString);

APDocument *doc = [APDocument documentWithXMLString:resultString];

APElement *rootElement = [doc rootElement];
NSArray *childElement = [rootElement childElements];

for (APElement *chile in childElement) {


    NSLog(@"chid name %@",chile.name);
    NSLog(@"chile value %@",chile.value);
}

这给了我这个结果..

 chid name soap:Body
 chile value (null)

【问题讨论】:

  • 那个 XML 元素没有价值,它只是有另一个孩子。
  • 如何获取子标签元素?谢谢
  • 你需要用勺子喂它吗?查看如何从根元素获取子元素并应用该逻辑。
  • @mohsinraza 看看我的回答。您只需对每个孩子递归调用childElements,直到他们不再有孩子

标签: iphone ios xml xml-parsing


【解决方案1】:

childElements 不是递归的,它只会返回根元素的直接后代。

所以在这种情况下,唯一的子元素是标签&lt;loginFunctionResponse xmlns="http://tempuri.org/"&gt;,其值为空,因为它没有任何内容,只有一个子元素。

如果你想解析整棵树,你应该对每个孩子递归调用childElements,直到你到达叶子(没有孩子的元素)。

【讨论】:

  • 我是 ios 的初学者,如果你给我链接任何示例如何解析整个树。谢谢
  • 这与 iOS 无关。这是一般的编程。
猜你喜欢
  • 2011-06-24
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-16
相关资源
最近更新 更多