XML parsing of iOS development

Source: Internet
Author: User

XML parsing 1.XML format for iOS development

Structure: Nested tree structure in layers

<?xml version= "1.0" encoding= "Utf-8"?>

1.0 represents the XML file version, Utf-8 represents the 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

2. How to use Gdata open Source Library to implement XML parsing common XML parsing method

(1) UNIX-LIBXML2 C interface

(2) Nsxmlparser UI provides an XML parsing library that is cumbersome to use

(3) Google development Gdata

2.1 Configuration
(1) Add header file search path    /      header search paths->/usr/include/libxml2    //(2) Add binary libraries    //  Link Library- >  libxml2.dylib    //(3) source file add compilation options    //      -fno-objc-arc    //(4) Add header file    //  #import " GDataXMLNode.h "

  

Use of 2.2 gdata

1. Load and parse XML file NSString *path = [[NSBundle mainbundle] pathforresource:@ "Xml.txt" oftype:nil];    NSData *data = [[NSData alloc] initwithcontentsoffile:path]; Gdataxmldocument represents an XML Document Object//Initwithdata using NSData initialization, which is parsing gdataxmldocument *doc = [[Gdataxmldocument alloc] In        Itwithdata:data options:0 Error:nil]; 2. Get the specified node XPath//cityname path:/root/systemconfig/cityname nsarray *array = [Doc nodesforxpath:@ "/root/systemconfig/    CityName "Error:nil";    CityName gdataxmlelement *element = [array firstobject];        NSLog (@ "name=%@ value=%@", element.name,element.stringvalue);    3. Gets the properties of the specified node Nsarray *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 *attr in item.attributes) {NSLog (@ "a-name=%@ avalue=%@", a    Ttr.name,attr.stringvalue); }//4. Gets all nodes of the specified name (regardless of location)//xpath syntax://item nsarray *allitem = [DOC nodesforxpath:@ "//item" error:nil];    For (Gdataxmlelement *e in Allitem) {NSLog (@ "name =%@", e.name);     }//5. Gets all properties of the specified name (regardless of which node)//xpath syntax://@value Nsarray *allvalue = [doc nodesforxpath:@ "//@value" error:nil];    For (Gdataxmlelement *e in Allvalue) {NSLog (@ "value =%@", e.stringvalue);    }//6. Iterate through the XML file//Get the root node gdataxmlelement *root = doc.rootelement; Get child node//root.children//Get child node number//root.childcount//Get child node of specified name//root elementsforname:<# (NSString *) #& Gt

  

XML parsing of iOS development

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.