XML parsing 1.XML format for iOS development
<?xml version= "1.0" encoding= "Utf-8"?> represents the XML file version, encoding used for internal text
<root> represents the root node
<CityName> Beijing </CityName> A node, CityName is the node name, Beijing node value
<item key= "1" value= "A" ></Item> key= "1" is a node attribute, key property name, "1" property value
Here is a copy of the XML file
<?XML version= "1.0" encoding= "Utf-8"?><Root> <Systemconfig> <CityName>Beijing</CityName> <Citycode>201</Citycode> <Parentcitycode>0</Parentcitycode> <AreaCode>010</AreaCode> <Agreementurl></Agreementurl> <Intentionlevel> <ItemKey= "1"value= "A"></Item> <ItemKey= "2"value= "B"></Item> <ItemKey= "3"value= "C"></Item> </Intentionlevel> <Comechannel> <ItemKey= "1"value= "Newspaper"></Item> <ItemKey= "2"value= "Magazine"></Item> </Comechannel> <Buycarbudget> <ItemKey= "1"value= "40.5 million"></Item> <ItemKey= "2"value= "50.6 million"></Item> </Buycarbudget> <Intentioncolor> <ItemKey= "1"value= "Red"></Item> <ItemKey= "2"value= "Yellow"></Item> </Intentioncolor> </Systemconfig></Root>
The following describes how to parse an XML file (XML parsing using the Gdata Open Source Library)
You need to configure the XML library before using Gdata
Configure the XML library (to use after configuration) //(1) Add header file search path // header search paths->/usr/include/libxml2 //(2) Add binary Library // Link Library -Lixml2.dylib //(3) source file add compilation options // -fno-objc-arc //(4) Add header file // #import "GDataXMLNode.h"
The following examples illustrate
//1. Loading and parsing XML filesNSString *path = [[NSBundle mainbundle] Pathforresource:@"Xml.txt"Oftype:nil]; NSData*data =[[NSData alloc] initwithcontentsoffile:path]; //gdataxmldocument represents an XML Document Object//Initwithdata using NSData initialization is the parsingGdataxmldocument *doc = [[Gdataxmldocument alloc] initwithdata:data options:0Error:nil]; //2. Get the specified node XPath//cityname Path:/root/systemconfig/citynameNsarray *array = [Doc Nodesforxpath:@"/root/systemconfig/cityname"Error:nil]; //CityNameGdataxmlelement *element =[Array firstobject]; NSLog (@"name=%@ value=%@", Element.name,element.stringvalue); //3. Get properties for a specified nodeNsarray *items = [Doc Nodesforxpath:@"/root/systemconfig/comechannel/item"Error:nil]; Gdataxmlelement*item =[Items Firstobject]; //gets the property that the property uses Gdataxmlelement to represent for(Gdataxmlelement *attrinchitem.attributes) {NSLog (@"a-name=%@ avalue=%@", Attr.name,attr.stringvalue); } //4. Get all nodes of the specified name (regardless of location)//XPath Syntax://ItemNsarray *allitem = [Doc Nodesforxpath:@"//item"Error:nil]; for(Gdataxmlelement *einchAllitem) {NSLog (@"name =%@", e.name); } //5. Get all properties of the specified name (regardless of node)//XPath Syntax://@valueNsarray *allvalue = [Doc Nodesforxpath:@"//@value"Error:nil]; for(Gdataxmlelement *einchallvalue) {NSLog (@"value =%@", E.stringvalue); } //6. Iterate through the XML file layer by row//Get root nodeGdataxmlelement *root =doc.rootelement; //Get child nodes//Root.children//gets the number of child nodes//Root.childcount//Gets the child node of the specified name//root elementsforname:<# (NSString *) #>
XML parsing of iOS development