XML for parsing of iOS data

Source: Internet
Author: User

Xml

1. Definition

    • Refers to Extensible Markup Language ()
    • XML HTML
    • XML to transfer data, not display data
    • XML XML is designed to be self-descriptive.
<?xml version="1.0" encoding="utf-8"?><users>    <User>        ID " 201502004 "> Zhang San </name>        <age></age>    </User>    <User>        ID"201502002"> John Doe </name>        <age>23 </age>    </User></Users>

2. Parsing

1) kissxml

* Need to import Kissxml library

* Go to build Settings, search the search box for head searching Path, then double click and click the + button to add/USR/INCLUDE/LIBXML2

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

- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //1. Get the XML addressNSString *path = [[NSBundle mainbundle]pathforresource:@"User"OfType:@"XML"]; //2. Get local urlstringNSString *content =[NSString stringwithcontentsofurl:[nsurl Fileurlwithpath:path] encoding:nsutf8stringencoding Error:nil]; //3. ParsingDdxmldocument *document = [[Ddxmldocument alloc]initwithxmlstring:content options:0Error:nil]; //4. Find the specified node through XPathNsarray *elements = [Document Nodesforxpath:@"/users/user"Error:nil]; //5. Traverse     for(Ddxmlelement *elementinchelements) {        //Fetch to nameDdxmlelement *name = [element elementsforname:@"name"][0]; //get the property of nameNsarray *node = [Document Nodesforxpath:@"/users/user/name"Error:nil]; Ddxmlnode*nodeid = [node[0] Attributeforname:@"ID"]; //Get AgeDdxmlelement *age = [element elementsforname:@" Age"][0]; NSLog (@"name =%@, age =%@, id =%@", [name Stringvalue],[age Stringvalue],[nodeid stringvalue]); }}

2) GData (MRC)

* Import Gdataxmlnode Library

* Find Build Ophases-> Compile Sources->GDATAXMLNODE.M Double click to add-fno-objc-arc

* Go to build Settings, search the search box for head searching Path, then double click and click the + button to add/USR/INCLUDE/LIBXML2

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

- (void) gdata{//1. Get PathNSString *path = [[NSBundle mainbundle]pathforresource:@"User"OfType:@"XML"]; //2. Get contentNSString *content =[NSString Stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil]; //3. Get the nodeGdataxmldocument *document = [[Gdataxmldocument alloc]initwithxmlstring:content options:0Error:nil]; Nsarray*elements = [Document Nodesforxpath:@"/users/user"Error:nil];  for(Gdataxmlelement *elementinchelements) {        //Fetch to nameGdataxmlelement *name = [element elementsforname:@"name"][0]; //get the property of nameNsarray *node = [Document Nodesforxpath:@"/users/user/name"Error:nil]; Gdataxmlelement*nodeid = [node[0] Attributeforname:@"ID"]; //Get AgeGdataxmlelement *age = [element elementsforname:@" Age"][0]; NSLog (@"name =%@, age =%@, id =%@", [name Stringvalue],[age Stringvalue],[nodeid stringvalue]); }}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}

3. Defining XML

* Using Kissxml

- (void) createxml{//1. Create a root elementDdxmlelement *rootelement = [[Ddxmlelement alloc] Initwithname:@"Users"];  for(inti =0; I <2; i++)    {        //2.User child elementsDdxmlelement *userelement = [[Ddxmlelement alloc] Initwithname:@"User"]; //name ElementDdxmlelement *nameelement = [[Ddxmlelement alloc] Initwithname:@"name"Stringvalue:[nsstring stringWithFormat:@"Zhang San%d", i+1]]; //Age ElementDdxmlelement *ageelement = [[Ddxmlelement alloc] Initwithname:@" Age"Stringvalue:[nsstring stringWithFormat:@"%d", -+I]]; //5. Adding child elements[Userelement addchild:nameelement];        [Userelement addchild:ageelement]; //6. Adding attributes to NameelementDdxmlnode *node = [Ddxmlnode attributewithname:@"ID"StringValue:@"xxxx"];        [Nameelement Addattribute:node]; //7. Add the user to the root element[RootElement addchild:userelement]; } NSLog (@"--%@", [rootelement description]);}

XML for parsing of iOS data

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.