簡體   English   中英

如何解析iPhone中的xml,

[英]how to parse xml in iPhone ,

我有包含xml responce的字符串,(asmx web服務)我正在通過APXML lib解析xml。 當我得到根元素給我身體標簽,我不想要身體標簽,我想得到我的實際數據,在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,因為它沒有任何內容,只有一個子元素。

如果你想解析整個樹,你應該遞歸地調用每個孩子的childElements ,直到你到達葉子(沒有孩子的元素)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM