gpt4 book ai didi

iphone - 如何在 iPhone 中解析 xml,

转载 作者:行者123 更新时间:2023-11-28 22:36:52 25 4
gpt4 key购买 nike

我有包含 xml 响应的字符串,(asmx 网络服务)我正在通过 APXML 库解析 xml。当我得到给我 body 标签的根元素时,我不想要 body 标签,我想得到我在 Root 标签而不是子标签中的实际数据..请告诉我如何从中获取数据..谢谢..我的 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)

最佳答案

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

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

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

关于iphone - 如何在 iPhone 中解析 xml,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15698439/

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