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:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <Users>
- <user id= "001" >
- <name> Cold Bamboo </name>
- <age>24</age>
- </User>
- <user id= "002" >
- <name>hzz</name>
- <age>23</age>
- </User>
- </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