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