iOS data parsing using gdataxml parsing, multilayer structure, the same directory method

Source: Internet
Author: User

Project examples (including Gdataxml framework)

http://download.csdn.net/detail/u013686641/7672689

/* Description

Bring Gdataxml to your project

Join the LIBXML2 framework

Import #import "GDataXMLNode.h"

In targets next, go to build Settings, search the search box for the header searches paths, add/USR/INCLUDE/LIBXML2

Then search for other linker flags, add-LXML2 the same way

Then mark the GDATAXMLNODE.M as not using arc:-FNO-OBJC-ARC

*/

//String to parse

    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> Costume </type_name><type_child><p_type><p_type _id>0</p_type_id><type_name> Women's </type_name><type_child><p_type><p_type_id >0</P_type_id><type_name> Dresses </type_name><type_child/></p_type><p_type> <P_type_id>0</P_type_id><type_name> Shorts </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> Dining </type_name><type_child><p_type><p_type_id>0</p_type_ Id><type_name>Szechuan </type_name><type_child><P_type><P_type_id>0</P_type_id><type_name> Hot Dishes </type_name><type_child/></p_type><p_type><p_type_id>0</p_type_id><type _name> Cold Dish </type_name><type_child/></p_type><p_type><p_type_id>0</p_type_id ><type_name> Snacks </type_name><type_child/></p_type></type_child></p_type> <P_type><P_type_id>0</P_type_id><type_name> Cantonese </type_name><type_child><p _type><p_type_id>0</p_type_id><type_name> Soup </type_name><type_child/></P_ Type><p_type><p_type_id>0</p_type_id><type_name> Porridge </type_name><type_child/ ></P_type><P_type><P_type_id>0</P_type_id><type_name> Snacks </type_name>< Type_child/></p_type></type_child></p_type><p_type><p_type_id>0</p_type_id ><type_name> Hunan </type_name><type_child><p_type><p_type_id>0</p_type_id><type_name > Hot Dishes </type_name><type_child/></p_type><p_type><p_type_id>0</p_type_id> <type_name> Snacks </type_name><type_child/></p_type></type_child></p_type><p _type><p_type_id>0</p_type_id><type_name> Shandong </type_name><type_child><p_type ><P_type_id>0</P_type_id><type_name> Hot Dishes </type_name><type_child/></p_type ><P_type><P_type_id>0</P_type_id><type_name> Cold dish </type_name><type_child/> </p_type></type_child></p_type></type_child></p_type></gettyperesult></ Gettyperesponse></soap:body></soap:envelope> ";

//Start parsing

gdataxmldocument *document = [[gdataxmldocumentalloc] initwithxmlstring: xmlstringoptions:0 error:nil];

//Get root node

gdataxmlelement *rootelement = [Document rootelement];

//trace to a valid parent node Gettyperesult

gdataxmlelement *soapbody=[[rootelement elementsforname:@ "Soap:body"] Objectatindex:0];

gdataxmlelement *gettyperesponse=[[soapbody elementsforname:@ "Gettyperesponse"] Objectatindex:0];

gdataxmlelement *gettyperesult=[[gettyperesponse elementsforname:@ "Gettyperesult"] objectatindex:0];

//Storage of First level p_type content, such as food and beverage

nsmutablearray *firstarr = [nsmutablearrayarraywithcapacity:0 ];

for (gdataxmlelement *element in [Gettyperesult elementsforname:@ "P_type"])

{

//Store A large class of information, such as food and beverage

nsmutabledictionary *firstdic = [nsmutabledictionarydictionarywithcapacity : 0 ];

//store First and class names

[FirstdicsetValue: [[Element elementsforname:@ "Type_name"] objectatindex: 0] stringvalue] forkey:@ "name"];

[FirstdicsetValue: [[Element elementsforname:@ "p_type_id"] objectatindex: 0] stringvalue] forkey:@ "id"];

//Get child nodes

gdataxmlelement *firstchild=[[element elementsforname:@ "Type_child"]Objectatindex :0];

//Storage Level two class array

nsmutablearray *secondarr = [nsmutablearrayarraywithcapacity:0 ];

for (gdataxmlelement *element in [FirstChild elementsforname:@ "P_type"])

{

//Store A level two class of information, such as Sichuan, etc.

nsmutabledictionary *seconddic = [nsmutabledictionarydictionarywithcapacity:0];

//Store the name of Class two

            [seconddic setvalue : [[[Element elementsforname : @ "Type_name" ] objectatindex : 0 ] stringvalue ] forkey : @ "name" ];

[SeconddicsetValue: [[Element elementsforname:@ "p_type_id"] objectatindex: 0] stringvalue] forkey:@ "id"];

//Get child nodes

gdataxmlelement *secondchild=[[element elementsforname:@ "Type_child"] Objectatindex:0];

//array with three levels of content

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);


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.