項目例子(包含GDataXML框架)
http://download.csdn.net/detail/u013686641/7672689
/*說明
將GDataXML引入你的項目
加入libxml2框架
導入#import "GDataXMLNode.h"
在TARGETS 接下來再進入Build Settings,在搜索框中搜索header search paths,添加/usr/include/libxml2
然後搜索Other linker flags,同樣的方式添加-lxml2
然後再將GDataXMLNode.m 標記爲不使用ARC: -fno-objc-arc
*/
//需要解析的字符串
NSString *xmlString=@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://xxxxxxx/2003/05/soap-envelope\" xmlns:xsi=\"http://xxxxx/2001/XMLSchema-instance\" xmlns:xsd=\"http://xxxxxx/2001/XMLSchema\"><soap:Body><GetTypeResponse xmlns=\"http://xxxx.org/\"><GetTypeResult><P_type><P_type_id>0</P_type_id><type_name>服飾</type_name><type_child><P_type><P_type_id>0</P_type_id><type_name>女裝</type_name><type_child><P_type><P_type_id>0</P_type_id><type_name>連衣裙</type_name><type_child /></P_type><P_type><P_type_id>0</P_type_id><type_name>短褲</type_name><type_child /></P_type></type_child></P_type></type_child></P_type><P_type><P_type_id>0</P_type_id><type_name>餐飲</type_name><type_child><P_type><P_type_id>0</P_type_id><type_name>川菜</type_name><type_child><P_type><P_type_id>0</P_type_id><type_name>熱菜</type_name><type_child /></P_type><P_type><P_type_id>0</P_type_id><type_name>涼菜</type_name><type_child /></P_type><P_type><P_type_id>0</P_type_id><type_name>小吃</type_name><type_child /></P_type></type_child></P_type><P_type><P_type_id>0</P_type_id><type_name>粵菜</type_name><type_child><P_type><P_type_id>0</P_type_id><type_name>湯</type_name><type_child /></P_type><P_type><P_type_id>0</P_type_id><type_name>粥</type_name><type_child /></P_type><P_type><P_type_id>0</P_type_id><type_name>小吃</type_name><type_child /></P_type></type_child></P_type><P_type><P_type_id>0</P_type_id><type_name>湘菜</type_name><type_child><P_type><P_type_id>0</P_type_id><type_name>熱菜</type_name><type_child /></P_type><P_type><P_type_id>0</P_type_id><type_name>小吃</type_name><type_child /></P_type></type_child></P_type><P_type><P_type_id>0</P_type_id><type_name>魯菜</type_name><type_child><P_type><P_type_id>0</P_type_id><type_name>熱菜</type_name><type_child /></P_type><P_type><P_type_id>0</P_type_id><type_name>涼菜</type_name><type_child /></P_type></type_child></P_type></type_child></P_type></GetTypeResult></GetTypeResponse></soap:Body></soap:Envelope>";
//開始解析
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];
//得到根節點
GDataXMLElement *rootElement = [document rootElement];
//追蹤到有效父節點 GetTypeResult
GDataXMLElement *soapBody=[[rootElement elementsForName:@"soap:Body"] objectAtIndex:0];
GDataXMLElement *getTypeResponse=[[soapBody elementsForName:@"GetTypeResponse"] objectAtIndex:0];
GDataXMLElement *getTypeResult=[[getTypeResponse elementsForName:@"GetTypeResult"] objectAtIndex:0];
//存放第一級P_type的內容,如餐飲等
NSMutableArray *firstArr = [NSMutableArray arrayWithCapacity:0];
for(GDataXMLElement *element in [getTypeResult elementsForName:@"P_type"])
{
//存放一個大類的信息,如餐飲等
NSMutableDictionary *firstDic = [NSMutableDictionary dictionaryWithCapacity:0];
//存放第一及類的名字
[firstDic setValue:[[[element elementsForName:@"type_name"] objectAtIndex:0] stringValue] forKey:@"name"];
[firstDic setValue:[[[element elementsForName:@"P_type_id"] objectAtIndex:0] stringValue] forKey:@"id"];
//得到子節點
GDataXMLElement *firstChild=[[element elementsForName:@"type_child"] objectAtIndex:0];
//存放二級類數組
NSMutableArray *secondArr = [NSMutableArray arrayWithCapacity:0];
for(GDataXMLElement *element in [firstChild elementsForName:@"P_type"])
{
//存放一個二級類的信息,如川菜等
NSMutableDictionary *secondDic = [NSMutableDictionary dictionaryWithCapacity:0];
//存放二級類的名字
[secondDic setValue:[[[element elementsForName:@"type_name"] objectAtIndex:0] stringValue] forKey:@"name"];
[secondDic setValue:[[[element elementsForName:@"P_type_id"] objectAtIndex:0] stringValue] forKey:@"id"];
//得到子節點
GDataXMLElement *secondChild=[[element elementsForName:@"type_child"] objectAtIndex:0];
//存放三級內容的數組
NSMutableArray *thirdArr=[NSMutableArray arrayWithCapacity:0];
for(GDataXMLElement *element in [secondChild elementsForName:@"P_type"])
{
NSMutableDictionary *thirdDic=[[NSMutableDictionary alloc] init];
[thirdDic setValue:[[[element elementsForName:@"type_name"] objectAtIndex:0] stringValue] forKey:@"name"];
[thirdDic setValue:[[[element elementsForName:@"P_type_id"] objectAtIndex:0] stringValue] forKey:@"id"];
[thirdArr addObject:thirdDic];
}
[secondDic setObject:thirdArr forKey:@"array"];
[secondArr addObject:secondDic];
}
[firstDic setObject:secondArr forKey:@"array"];
[firstArr addObject:firstDic];
}
NSLog(@"----------------------------%@",firstArr);