[size=medium]最近项目用到了从服务器获取xml文件,需要解析xml来获取数据,下面简单举个例子,以便自己以后学习并复习。另外[url]http://www.w3school.com.cn/xpath/xpath_examples.asp[/url]上有一些例子,有兴趣的同学可以去看看xml的相关知识。
从服务器上下载的xml文件:[/size]
[size=medium]上面的xml中主要需要两个循环,一个:ROWSET下的ROW节点 ,一个是:SEGINFO下的SEGINFO_ROW节点
UserOrder *orderObj 自己定义的数据结构,用来存放xml数据[/size]
从服务器上下载的xml文件:[/size]
<?xml version="1.0" encoding="utf-8"?>
<page>
<ROWSET>
<ROW num="1"><!-- 订单1 -->
<ORDERNO>C1007260000037</ORDERNO><!-- 订单号,选填 -->
<STATUS>E</STATUS><!-- 订单状态 -->
<TOTALPAYMONEY>6810</TOTALPAYMONEY><!-- 订单总价 -->
<CREATEDATE>2010-07-26</CREATEDATE><!-- 订单创建日期 -->
<CAMPAIGNSCRIPTID>6</CAMPAIGNSCRIPTID><!-- 订单类型标识 -->
<SEGINFO><!-- 订单航段 -->
<SEGINFO_ROW num="1"><!-- 订单航段1 -->
<CARRIER>CZ</CARRIER>
<DEPCODE>CAN</DEPCODE>
<ARRCODE>PEK</ARRCODE>
<DEPPORT> 广州</DEPPORT>
<ARRPORT> 北京</ARRPORT>
<CARRIER>CZ</CARRIER>
<FLIGHTNO>3107</FLIGHTNO>
<DEPTIME>2010-07-28</DEPTIME>
<ARRTIME>2010-07-28</ARRTIME>
</SEGINFO_ROW>
<SEGINFO_ROW num="2"><!-- 订单航段2 -->
<CARRIER>CZ</CARRIER>
<DEPCODE>PEK</DEPCODE>
<ARRCODE>CAN</ARRCODE>
<DEPPORT> 北京</DEPPORT>
<ARRPORT> 广州</ARRPORT>
<CARRIER>CZ</CARRIER>
<FLIGHTNO>3104</FLIGHTNO>
<DEPTIME>2010-07-29</DEPTIME>
<ARRTIME>2010-07-29</ARRTIME>
</SEGINFO_ROW>
</SEGINFO>
</ROW>
<ROW num="2">
<ORDERNO>C1007260000034</ORDERNO>
<DOMESTICINDICATE>1</DOMESTICINDICATE>
<STATUS>C</STATUS>
<TOTALPAYMONEY>2010</TOTALPAYMONEY>
<CREATEDATE>2010-07-26</CREATEDATE>
<CAMPAIGNSCRIPTID>6</CAMPAIGNSCRIPTID>
<SEGINFO>
<SEGINFO_ROW num="1">
<CARRIER>CZ</CARRIER>
<DEPCODE>CAN</DEPCODE>
<ARRCODE>PEK</ARRCODE>
<DEPPORT> 广州</DEPPORT>
<ARRPORT> 北京</ARRPORT>
<CARRIER>CZ</CARRIER>
<FLIGHTNO>3107</FLIGHTNO>
<DEPTIME>2010-07-28</DEPTIME>
<ARRTIME>2010-07-28</ARRTIME>
</SEGINFO_ROW>
</SEGINFO>
</ROW>
</ROWSET>
</page>
[size=medium]上面的xml中主要需要两个循环,一个:ROWSET下的ROW节点 ,一个是:SEGINFO下的SEGINFO_ROW节点
UserOrder *orderObj 自己定义的数据结构,用来存放xml数据[/size]
//初始化数据源,从(NSData*)aData参数中
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:aData options:0 error:&error];
NSMutableArray *orderArray = [[NSMutableArray alloc] initWithCapacity:2];
NSArray *orders = [doc nodesForXPath:@"//ROW" error:aError];//循环ROW节点
for (GDataXMLElement *order in orders) {
UserOrder *orderObj = [[UserOrder alloc] init];
//获取ROW节点下节点数据
orderObj.orderNo = [order stringForTextNodeNamed:@"ORDERNO"];
orderObj.status = [order stringForTextNodeNamed:@"STATUS"];
orderObj.price = [order stringForTextNodeNamed:@"TOTALPAYMONEY"];
orderObj.date = [order stringForTextNodeNamed:@"CREATEDATE"];
orderObj.orderType = [order stringForTextNodeNamed:@"CAMPAIGNSCRIPTID"];
NSArray *segmentElementArray = [order nodesForXPath:@"SEGINFO/SEGINFO_ROW" error:aError];//循环SEGINFO_ROW节点
if (segmentElementArray != nil) {
for (GDataXMLElement *segmentElement in segmentElementArray) {
OrderSegment *segment = [[OrderSegment alloc] init];
//获取SEGINFO_ROW节点下数据
segment.carrier = [segmentElement stringForTextNodeNamed:@"CARRIER"];
segment.flightType = [segmentElement stringForTextNodeNamed:@"FLIGHTNO"];
segment.depCode = [segmentElement stringForTextNodeNamed:@"DEPCODE"];
segment.arrCode = [segmentElement stringForTextNodeNamed:@"ARRCODE"];
segment.depPort = [segmentElement stringForTextNodeNamed:@"DEPPORT"];
segment.arrPort = [segmentElement stringForTextNodeNamed:@"ARRPORT"];
segment.flightNo = [segmentElement stringForTextNodeNamed:@"FLIGHTNO"];
segment.depTime = [segmentElement stringForTextNodeNamed:@"DEPTIME"];
segment.arrTime = [segmentElement stringForTextNodeNamed:@"ARRTIME"];
[orderObj.segments addObject:segment];
[segment release];
}
[orderArray addObject:orderObj];
}
[orderObj release];
}
return [orderArray autorelease];//返回数据