XML parsing of iOS

Source: Internet
Author: User

iOS XML parsing and pull-up refresh pull-up load 1.XML format

Here is a sample code for XML,

<?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>

<?xml version= "1.0" encoding= "Utf-8"?>  XML document declaration, which declares the type of the XML document, indicating the version of the document and how the document is encoded
<CityName> Beijing </CityName> represents an element in XML,<cityname> represents the start tag of an element,</cityname> represents the end tag of an element. Beijing indicates the content of the element
<item key= "1" value= "Red" ></Item> represents an element with multiple attributes, Item has key and value two properties, corresponding content is 1 and red
2. Parsing an XML file using Gdata

There are two ways to parse XML:

(1) DOM: Loading an entire XML document into memory at once, which is more suitable for parsing small files

(2) SAX: Starting from the root element, in order to parse an element down, it is more suitable for parsing large files

XML parsing methods in iOS:

(1) Apple native Nsxmlparser:sax method analysis, easy to use

(2) Third party framework

1) LIBXML2: pure C language, which is included by default in the iOS SDK while supporting DOM and sax parsing

2) Gdataxml:dom method Analysis, developed by Google, based on LIBXML2

I'll use Gdata to parse the XML above

Step One: Add Gdata project files to Xcode's engineering files (note the copy items if needed on add)

   

Step:

//Configure the XML library (to use after configuration)//(1) Add header file search path//Select the project, type search in the search field of the Bulid settings, and//Click Header Search Paths to add/USR/INCLUDE/LIBXML2//(2) Adding a binary library//In the link Binary with Librarys of build phases//Add Lixml2.dylib//(3) source file Add compilation option//find Gdata related files in the compile sources of build phases//Click Add-fno-objc-arc//(4) Add header file//#import "GDataXMLNode.h"

   

  

SETP Three:

Demo of the Code:

XML is a layer of nested tree structure, through the structure of the path can be traversed to query the contents of each node, for example:/root/systemconfig/cityname path point to CityName node, By Gdataxmldocument Method Nodesforxpath: (NSString *) path error: (nserror) *error can get the element of the specified node, through the node element's Name property can get the node name, Here, CityName, the node's contents can be obtained through the StringValue attribute of the node element, which is Beijing; When a node contains multiple elements, the Forin syntax traversal is taken after the node is fetched, and the example code 3rd is referred to in the following procedure. When you want to take out a node in an XML file, Will Nodesforxpath: (NSString *) path error: (nserror) *error The path parameter set//item,item for the node you want to remove the reference instance code fourth 5th More XPath syntax can refer to URL http://www.w3school.com.cn/xpath/

    //1. Loading and parsing XML filesNSString*path = [[NSBundle mainbundle] Pathforresource:@"XML"OfType:@"txt"]; //gdataxmldocument Identifying XML Document Objects//Use Initwithdata with NSData initialization, that is, parsingNSData *data =[[NSData alloc] initwithcontentsoffile:path]; Gdataxmldocument*doc = [[Gdataxmldocument alloc] initwithdata:data options:0Error:nil]; //2. Get the specified node XPath//cityname Path:/root/systemconfig/citynameNsarray*arr = [Doc Nodesforxpath:@"/root/systemconfig/cityname"Error:nil]; Gdataxmlelement*element =[arr Firstobject]; NSLog (@"name =%@ value =%@", Element.name,element.stringvalue); //3. Get properties for a specified nodeNsarray *arr2 = [Doc Nodesforxpath:@"/root/systemconfig/intentionlevel/item"Error:nil]; NSLog (@"I am here%ld", Arr2.count); //Get Properties     for(Gdataxmlelement *elementincharr2) {         for(Gdataxmlelement *attrinchelement.attributes) {NSLog (@"I am%@ and value%@", 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 *itemsinchAllitem) {//NSLog (@ "name =%@", items.name);         for(Gdataxmlelement *attrinchitems.attributes) {NSLog (@"name =%@ value =%@", Attr.name,attr.stringvalue); }    }        //5. Get all attributes of the specified name (no matter which node)//XPath Syntax://@valueNsarray *allvalue = [Doc Nodesforxpath:@"//@value"Error:nil];  for(Gdataxmlelement *valuesinchallvalue) {NSLog (@"value =%@", Values.stringvalue); } Nsarray*allkey = [Doc Nodesforxpath:@"//@key"Error:nil];  for(Gdataxmlelement *keysinchAllkey) {NSLog (@"key =%@", Keys.stringvalue); }

XML parsing of iOS

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.