Gdataxmlnode create and parse XML

Source: Internet
Author: User
Gdataxmlnode creates XML:

# Import <Foundation/Foundation. h> 2 # import "gdataxmlnode. H "3 4 int main () 5 {6 nserror * error; 7 // create the root node 8 gdataxmlelement * rootelement = [gdataxmlnode elementwithname: @" ABC "]; 9 // create the first subnode 10 gdataxmlelement * element = [gdataxmlnode elementwithname: @ "name" stringvalue: @ ""]; 12 // Add the child node to the root node 13 [rootelement addchild: element]; 14 15 // use the root node to create the XML document 16 gdataxmldocument * rootdoc = [[gdataxmldocument alloc] initwithrootelement: rootelement]; 17 // set the XML version used 18 [rootdoc setversion: @ "1.0"]; 19 // set the character encoding of the XML document 20 [rootdoc setcharacterencoding: @ "UTF-8"]; 21 22 // obtain and print the XML string 23 nsstring * STR = [[nsstring alloc] initwithdata: rootdoc. xmldata encoding: nsutf8stringencoding]; 24 nslog (@ "% @", STR); 25 26 return 0; 27}

 

The compilation command is:

1 clang main.m GDataXMLNode.m -framework Foundation -lxml2 -I /usr/include/libxml2/

Because xml2 is used in gdataxmlnode, remember to connect it and specify the header file search path. You must also remember to use setcharacterencoding to specify the character set.

 

 

Bytes -------------------------------------------------------------------------------------------------------------------------

City. xml

<Province>

<Hebei>

<City> Shijiazhuang </city>

<City> Tangshan </city>

<City> Qinhuangdao </city>

<City> Handan </city>

<City> Xingtai </city>

<City> Baoding </city>

<City> Chengde </city>

<City> Cangzhou </city>

<City> Langfang </city>

<City> Hengshui </city>

</Hebei>

<Henan>

<City> Zhengzhou </city>

<City> Kaifeng </city>

<City> Luoyang </city>

<City> Pingdingshan </city>

<City> Anyang </city>

<City> hebi </city>

<City> Xinxiang </city>

<City> Jiaozuo </city>

<City> Puyang </city>

<City> xuchang </city>

<City> Luohe </city>

<City> Sanmenxia </city>

<City> Nanyang </city>

<City> Shangqiu </city>

<City> Xinyang </city>

<City> Zhoukou </city>

<City> Zhumadian </city>

</Henan>

</Province>

Gdataxmldata parsing XML: 1. Method 1:

-(Void) getdata

{

// A dictionary

Self. DIC = [[nsmutabledictionary alloc] initwithcapacity: 10];

// Obtain the city. XML Path in the package

Nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "city" oftype: @ "XML"];

// Binary data

Nsdata * Data = [[nsdata alloc] initwithcontentsoffile: path];

// Gdataxmldocument starts parsing XML

Gdataxmldocument * Document = [[gdataxmldocument alloc] initwithdata: Data options: 0 error: Nil]

// Obtain the root node

Gdataxmlelement * root = Document. rootelement;

// All subnodes under the root node (excluding the grandson node)

Nsarray * rootson = [root children];

// Traverse the child nodes of the Root Node

For (gdataxmlelement * element in rootson ){

// All cities are put into the dictionary by the province name as the key

Nsmutablearray * ary = [self. DIC objectforkey: element. Name];

If (ary = nil ){

Ary = [[nsmutablearray alloc] initwithcapacity: 20];

[Self. DIC setobject: ary forkey: element. Name];

}

Nsarray * citys = element. Children;

For (gdataxmlelement * city in citys ){

// [Self. DIC setobject: City. stringvalue forkey: City. Name];

[Ary addobject: City. stringvalue];

}

}

}

2. method 2

    

-(Void) getdata1

{

Self. DIC = [[nsmutabledictionary alloc] initwithcapacity: 10];

Nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "city" oftype: @ "XML"];

Nsdata * Data = [[nsdata alloc] initwithcontentsoffile: path];

Gdataxmldocument * Document = [[gdataxmldocument alloc] initwithdata: Data options: 0 error: Nil];

Gdataxmlelement * root = Document. rootelement;

// Parse the following to start this method using nodesforxpath

Nsarray * prov = @ [@ "Henan", @ "Hebei"];

For (INT I = 0; I <[prov count]; I ++)

{

// You can use nodesforxpath to obtain all nodes in the // % @/city format under the root node (root node or root node,

// % @ Is Henan, Hebei, etc. In this format, obtain all city nodes (which can be Sun Tzu nodes). The limit is that the parent node of the city node must be % @

Nsarray * array = [root nodesforxpath: [nsstring stringwithformat: @ "// % @/City", prov [I] error: Nil];

// Obtain all the cities in Henan (or Hebei) and put them in the dictionary with Henan (Hebei) as the key

For (gdataxmlelement * element in array ){

Nsmutablearray * ary = [self. DIC objectforkey: prov [I];

If (ary = nil ){

Ary = [[nsmutablearray alloc] initwithcapacity: 20];

[Self. DIC setobject: ary forkey: prov [I];

}

[Ary addobject: element. stringvalue];

}

}

}

Note:

The elementsforname method can also get the node by name, which is similar to nodesforxpath, but only limited to obtaining the child node.

 

 

 

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.