0

我有包含 xml 响应的字符串,(asmx Web 服务)我正在通过 APXML 库解析 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)
4

1 回答 1

2

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

所以在这种情况下,唯一的子元素是 tag <loginFunctionResponse xmlns="http://tempuri.org/">,它的值为 null 因为它没有任何内容,只有一个子元素。

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

于 2013-03-29T06:31:30.643 回答