Ios-gdata Parsing xml

Source: Internet
Author: User

IOS GData Parsing XML Summary

There are many ways to parse XML on the iOS platform, and the parsing method that comes with it in the SDK. But we are more inclined to use third-party libraries because of the high resolution and ease of use. The following is a description of the Open Source Library gdata parsing xml under Google.

can go to http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/XMLSupport/download source code, Download it down and go to the folder to find the Xmlsupport folder, Drag inside the GDataXMLNode.h and GDATAXMLNODE.M files to the new folder in the project (I'm here to build the Gdataxml folder), and note to select the copy file into the project instead of just the reference,

To configure the project, click the project root and click Target on the left, go to build phases, then click the third link binary with libraries, click the plus sign to search for LIBXML2 and add the library to the project,

Then go to build Settings, search the search box for head searching Path, then double click and click the + button to add/usr/include/libxml2,

Next search box search for other linker flags, the same way to add-lxml2,

The work is done (it's a bit of a hassle) and then we'll see how to use it:

First create a new XML file in the project, as the object we want to parse, the new method is to create a new empty file in the project, named Users.xml, and then add the content:

    1. <?xml version= "1.0" encoding= "Utf-8"?>
    2. <Users>
    3. <user id= "001" >
    4. <name> Cold Bamboo </name>
    5. <age>24</age>
    6. </User>
    7. <user id= "002" >
    8. <name>hzz</name>
    9. <age>23</age>
    10. </User>
    11. </Users>

Next you can start parsing, introducing the header file into the file that needs to be parsed: #import "GDataXMLNode.h"

I am a new empty project, so I use it directly in APPDELEGATE.M, the code is as follows:

#import "XRAppDelegate.h"

#import "GDataXMLNode.h"

@implementation Xrappdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions

{

Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];

Get the XML file under the project directory

NSString * FilePath = [[NSBundle mainbundle] pathforresource:@ "Users" oftype:@ "xml"];

Convert the contents of an XML file to a NSData type

NSData * data = [NSData Datawithcontentsoffile:filepath];

Initializing a Gdata object to create a document tree

Gdataxmldocument * doc = [[Gdataxmldocument alloc] Initwithdata:data options:0 Error:nil];

Get root node (Users)

Gdataxmlelement * rootelement = [doc rootelement];

Nsarray * users = [rootelement elementsforname:@ "User"];

Traversing nodes

For (gdataxmlelement * user in users) {

Gets the id attribute of the user node

NSString * userId = [[user attributeforname:@ "id"] stringvalue];

Gets the value of the name node

Gdataxmlelement * nameelement = [[User elementsforname:@ "name"] objectatindex:0];

NSString * name = [Nameelement stringvalue];

Gets the value of the age node

Gdataxmlelement * ageelement = [[User elementsforname:@ "Age"] objectatindex:0];

NSString * age = [ageelement stringvalue];

NSLog (@ "userid:%@", UserID);

NSLog (@ "Name:%@", name);

NSLog (@ "Age:%@", age);

}

Self.window.backgroundColor = [Uicolor Whitecolor];

[Self.window makekeyandvisible];

return YES;

}

END

Ios-gdata Parsing xml

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.