IOS learning: Common third-party libraries (GDataXMLNode: xml Parsing Library)

Source: Internet
Author: User

1. GDataXMLNode description


GDataXMLNode is a class set provided by Google for XML data processing. This type of set encapsulates the libxml2-DOM processing method. It can read and write small or medium-sized xml documents and supports the XPath syntax.


Usage:
1. Obtain the GDataXMLNode. h/m file and add the GDataXMLNode. h/m file to the project.
2. Add the "libxml2.dylib" Library to the project.
3. On the "Build Settings" page of the project, find the "Header Search Path" and add/usr/include/libxml2 "to the Path.
4. Add the "GDataXMLNode. h" file to the header file. If the project can be compiled, the GDataXMLNode is successfully added.


Ii. GDataXMLNode example


Example:
[Html]
<Root>
<Name value = "wusj"/>
<Age> 24 </age>
</Root>

<Root>
<Name value = "wusj"/>
<Age> 24 </age>
</Root>

Parse this xml file


[Cpp] NSString * xmlPath = [[NSBundlemainBundle] pathForResource: @ "test" ofType: @ "xml"];
NSString * xmlString = [NSStringstringWithContentsOfFile: xmlPath encoding: NSUTF8StringEncodingerror: nil];
GDataXMLDocument * xmlDoc = [[GDataXMLDocumentalloc] initWithXMLString: xmlString options: 0 error: nil];
GDataXMLElement * xmlEle = [xmlDoc rootElement];
NSArray * array = [xmlEle children];
NSLog (@ "count: % d", [array count]);

For (int I = 0; I <[array count]; I ++ ){
GDataXMLElement * ele = [array objectAtIndex: I];

// Judge by Tag Name
If ([[ele name] isw.tostring: @ "name"]) {
// Read the attributes in the tag
NSLog (@ "name --> % @", [[ele attributeForName: @ "value"] stringValue]);
} Else {
// Directly read the String between tags
NSLog (@ "age --> % @", [ele stringValue]);
}

}

NSString * xmlPath = [[NSBundlemainBundle] pathForResource: @ "test" ofType: @ "xml"];
NSString * xmlString = [NSStringstringWithContentsOfFile: xmlPath encoding: NSUTF8StringEncodingerror: nil];
GDataXMLDocument * xmlDoc = [[GDataXMLDocumentalloc] initWithXMLString: xmlString options: 0 error: nil];
GDataXMLElement * xmlEle = [xmlDoc rootElement];
NSArray * array = [xmlEle children];
NSLog (@ "count: % d", [array count]);

For (int I = 0; I <[array count]; I ++ ){
GDataXMLElement * ele = [array objectAtIndex: I];

// Judge by Tag Name
If ([[ele name] isw.tostring: @ "name"]) {
// Read the attributes in the tag
NSLog (@ "name --> % @", [[ele attributeForName: @ "value"] stringValue]);
} Else {
// Directly read the String between tags
NSLog (@ "age --> % @", [ele stringValue]);
}

}

 

Running result:

 

Iii. GDataXMLNode method summary


The final data reading is done in the GDataXMLElement object. The following methods are the methods of the GDataXMLElement class:
1. name method, take the name of the e. g name label "name"
2. attributeForName: Get the value attribute in the attribute value e. g name label by calling stringValue.
3. stringValue: string value between tags e. g: 24


 

Related Article

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.