IOS資料解析_使用GDataXML解析,多層結構,相同目錄的方法

來源:互聯網
上載者:User

標籤:ios   xml   資料   

項目例子(包含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  = [[GDataXMLDocumentalloc] initWithXMLString:xmlStringoptions: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 = [NSMutableArrayarrayWithCapacity:0];

    for(GDataXMLElement *element in [getTypeResult elementsForName:@"P_type"])

    {

        //存放一個大類的資訊,如餐飲等

        NSMutableDictionary *firstDic = [NSMutableDictionarydictionaryWithCapacity:0];

        

        //存放第一及類的名字

        [firstDicsetValue:[[[element elementsForName:@"type_name"] objectAtIndex:0] stringValue] forKey:@"name"];

        [firstDicsetValue:[[[element elementsForName:@"P_type_id"] objectAtIndex:0] stringValue] forKey:@"id"];

        //得到子節點

        GDataXMLElement *firstChild=[[element elementsForName:@"type_child"]objectAtIndex:0];

        

        //存放二級類數組

        NSMutableArray *secondArr = [NSMutableArrayarrayWithCapacity:0];

        for(GDataXMLElement *element in [firstChild elementsForName:@"P_type"])

        {

            //存放一個二級類的資訊,如川菜等

            NSMutableDictionary *secondDic = [NSMutableDictionarydictionaryWithCapacity:0];

            //存放二級類的名字

            [secondDicsetValue:[[[element elementsForName:@"type_name"] objectAtIndex:0] stringValue] forKey:@"name"];

            [secondDicsetValue:[[[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=[[NSMutableDictionaryalloc] init];

                [thirdDicsetValue:[[[element elementsForName:@"type_name"] objectAtIndex:0] stringValue] forKey:@"name"];

                [thirdDicsetValue:[[[element elementsForName:@"P_type_id"] objectAtIndex:0] stringValue] forKey:@"id"];

                [thirdArraddObject:thirdDic];

            }

            [secondDicsetObject:thirdArr forKey:@"array"];

            [secondArraddObject:secondDic];

        }

        

        [firstDicsetObject:secondArr forKey:@"array"];

        [firstArraddObject:firstDic];

    }

    

    NSLog(@"----------------------------%@",firstArr);


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.