objective-c XML循环解析

最近项目用到了从服务器获取xml文件,需要解析xml来获取数据,下面简单举个例子,以便自己以后学习并复习。另外http://www.w3school.com.cn/xpath/xpath_examples.asp上有一些例子,有兴趣的同学可以去看看xml的相关知识。 
从服务器上下载的xml文件:
 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <page>  
  3.     <ROWSET>  
  4.         <ROW num="1"><!--   订单1  -->  
  5.             <ORDERNO>C1007260000037</ORDERNO><!--   订单号,选填  -->  
  6.             <STATUS>E</STATUS><!--   订单状态  -->  
  7.             <TOTALPAYMONEY>6810</TOTALPAYMONEY><!--   订单总价  -->  
  8.             <CREATEDATE>2010-07-26</CREATEDATE><!--   订单创建日期  -->  
  9.             <CAMPAIGNSCRIPTID>6</CAMPAIGNSCRIPTID><!--  订单类型标识  -->  
  10.             <SEGINFO><!--   订单航段  -->  
  11.                 <SEGINFO_ROW num="1"><!--   订单航段1  -->  
  12.                     <CARRIER>CZ</CARRIER>  
  13.                     <DEPCODE>CAN</DEPCODE>  
  14.                     <ARRCODE>PEK</ARRCODE>  
  15.                     <DEPPORT> 广州</DEPPORT>  
  16.                     <ARRPORT> 北京</ARRPORT>  
  17.                     <CARRIER>CZ</CARRIER>  
  18.                     <FLIGHTNO>3107</FLIGHTNO>  
  19.                     <DEPTIME>2010-07-28</DEPTIME>  
  20.                     <ARRTIME>2010-07-28</ARRTIME>  
  21.                 </SEGINFO_ROW>  
  22.                 <SEGINFO_ROW num="2"><!--   订单航段2  -->  
  23.                     <CARRIER>CZ</CARRIER>  
  24.                     <DEPCODE>PEK</DEPCODE>  
  25.                     <ARRCODE>CAN</ARRCODE>  
  26.                     <DEPPORT> 北京</DEPPORT>  
  27.                     <ARRPORT> 广州</ARRPORT>  
  28.                     <CARRIER>CZ</CARRIER>  
  29.                     <FLIGHTNO>3104</FLIGHTNO>  
  30.                     <DEPTIME>2010-07-29</DEPTIME>  
  31.                     <ARRTIME>2010-07-29</ARRTIME>  
  32.                 </SEGINFO_ROW>  
  33.             </SEGINFO>  
  34.         </ROW>  
  35.         <ROW num="2">  
  36.             <ORDERNO>C1007260000034</ORDERNO>  
  37.             <DOMESTICINDICATE>1</DOMESTICINDICATE>  
  38.             <STATUS>C</STATUS>  
  39.             <TOTALPAYMONEY>2010</TOTALPAYMONEY>  
  40.             <CREATEDATE>2010-07-26</CREATEDATE>  
  41.             <CAMPAIGNSCRIPTID>6</CAMPAIGNSCRIPTID>  
  42.             <SEGINFO>  
  43.                 <SEGINFO_ROW num="1">  
  44.                     <CARRIER>CZ</CARRIER>  
  45.                     <DEPCODE>CAN</DEPCODE>  
  46.                     <ARRCODE>PEK</ARRCODE>  
  47.                     <DEPPORT> 广州</DEPPORT>  
  48.                     <ARRPORT> 北京</ARRPORT>  
  49.                     <CARRIER>CZ</CARRIER>  
  50.                     <FLIGHTNO>3107</FLIGHTNO>  
  51.                     <DEPTIME>2010-07-28</DEPTIME>  
  52.                     <ARRTIME>2010-07-28</ARRTIME>  
  53.                 </SEGINFO_ROW>  
  54.             </SEGINFO>  
  55.         </ROW>  
  56.     </ROWSET>  
  57. </page>  
上面的xml中主要需要两个循环,一个:ROWSET下的ROW节点 ,一个是:SEGINFO下的SEGINFO_ROW节点  UserOrder *orderObj 自己定义的数据结构,用来存放xml数据 
Objective-c代码  收藏代码
  1. //初始化数据源,从(NSData*)aData参数中  
  2. GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:aData options:0 error:&error];  
  3.   
  4. NSMutableArray *orderArray = [[NSMutableArray alloc] initWithCapacity:2];  
  5.       
  6.     NSArray *orders = [doc nodesForXPath:@"//ROW" error:aError];//循环ROW节点  
  7.     for (GDataXMLElement *order in orders) {  
  8.           
  9.         UserOrder *orderObj = [[UserOrder alloc] init];  
  10.           
  11. //获取ROW节点下节点数据  
  12.         orderObj.orderNo = [order stringForTextNodeNamed:@"ORDERNO"];  
  13.         orderObj.status = [order stringForTextNodeNamed:@"STATUS"];  
  14.         orderObj.price = [order stringForTextNodeNamed:@"TOTALPAYMONEY"];  
  15.         orderObj.date = [order stringForTextNodeNamed:@"CREATEDATE"];  
  16.         orderObj.orderType = [order stringForTextNodeNamed:@"CAMPAIGNSCRIPTID"];  
  17.           
  18.           
  19.         NSArray *segmentElementArray = [order nodesForXPath:@"SEGINFO/SEGINFO_ROW" error:aError];//循环SEGINFO_ROW节点  
  20.         if (segmentElementArray != nil) {  
  21.               
  22.             for (GDataXMLElement *segmentElement in segmentElementArray) {  
  23.                   
  24.                 OrderSegment *segment = [[OrderSegment alloc] init];  
  25.                   
  26. //获取SEGINFO_ROW节点下数据  
  27.                 segment.carrier = [segmentElement stringForTextNodeNamed:@"CARRIER"];  
  28.                 segment.flightType = [segmentElement stringForTextNodeNamed:@"FLIGHTNO"];  
  29.                 segment.depCode = [segmentElement stringForTextNodeNamed:@"DEPCODE"];  
  30.                 segment.arrCode = [segmentElement stringForTextNodeNamed:@"ARRCODE"];   
  31.                 segment.depPort = [segmentElement stringForTextNodeNamed:@"DEPPORT"];  
  32.                 segment.arrPort = [segmentElement stringForTextNodeNamed:@"ARRPORT"];  
  33.                 segment.flightNo = [segmentElement stringForTextNodeNamed:@"FLIGHTNO"];  
  34.                 segment.depTime = [segmentElement stringForTextNodeNamed:@"DEPTIME"];  
  35.                 segment.arrTime = [segmentElement stringForTextNodeNamed:@"ARRTIME"];  
  36.                   
  37.                 [orderObj.segments addObject:segment];  
  38.                 [segment release];  
  39.                   
  40.             }  
  41.               
  42.             [orderArray addObject:orderObj];  
  43.         }  
  44.         [orderObj release];  
  45.           
  46.     }  
  47.     return [orderArray autorelease];//返回数据  
posted @ 2014-04-28 20:29  萧萧  阅读(326)  评论(0)    收藏  举报