IOS DOM parsing XML format data

Source: Internet
Author: User

Problem Description: The interface returns XML format data, and the node name is actually the same, using the DOM parsing the final Solution

I. File import

1. Download GDataXMLNode.h and GDATAXMLNODE.M files, import project (csdn file download link: http://download.csdn.net/detail/wusangtongxue/9502292)

2. Configure the Environment:

(1) Change into ARC environment (-FNO-OBJC-ARC);

(2) Find the "Paths\header Search Paths" entry and add "/USR/INCLUDE/LIBXML2" to the list;

(3) Find the "Linking\other Linker Flags" entry and add "-LXML2" to the list

3. Import the Sbjson folder to convert the parsed data to a string type

Two. Actual Project

1. XML Format data for Web page printing

2. Import #import "GDataXMLNode.h", parse

-(void) xmlhttprequest{NSString*str =Intranet website; Nsurl*urll =[Nsurl Urlwithstring:str]; Nsmutableurlrequest*request = [Nsmutableurlrequest requestwithurl:urll cachepolicy:nsurlrequestuseprotocolcachepolicy TimeoutInterval:Ten]; //2.[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {Nserror*errror =Nil; Gdataxmldocument*document = [[Gdataxmldocument alloc]initwithdata:data options:0error:&Errror]; //Get root nodeGdataxmlelement *rootelement =[Document RootElement]; //gets all child nodes loaded into an arrayNsarray *array = [rootelement elementsforname:@"string"]; NSLog (@"Print array:%@", array); NSString*string=[nsstring stringWithFormat:@"%@", array[2]]; Nsrange Startstr= [stringRangeofstring:@"<string>"]; Nsrange End= [stringRangeofstring:@"</string>"]; NSString*sub = [stringSubstringwithrange:nsmakerange (startstr.location+8, end.location-startstr.location-9)]; NSLog (@"Print the truncated string:%@", sub); }];}

Array: Print Arrays: (
"Gdataxmlelement 0x7ff26ad97f10: {type:1 name:string xml:\" <string>610</string>\ "}",
"Gdataxmlelement 0x7ff26ad97cb0: {type:1 name:string xml:\" <string/>\ "}",
"Gdataxmlelement 0x7ff26ad97ce0: {type:1 name:string xml:\" <string>http://192.168.1.1:8080/</string>\ " }",
"Gdataxmlelement 0x7ff26adbb480: {type:1 name:string xml:\" <string/>\ "}"
)
Sub: Print the truncated string: http://192.168.1.1:8080
So we can get nsstring data.

Note : The URL is the intranet, need to be based on their own needs to parse the example here is the XML data of my project, a bit special is the node name is the same

Reference URL: http://www.cnblogs.com/pengyingh/articles/2342164.html

IOS DOM parsing XML format 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.