1.首先得包含该文件。
GDataXMLNode.h
GDataXMLNode.m
(google的那有)
2.在build phases 中包含
libxml2.dylib库。
3.build settings 设置 header search paths:
${SDK_DIR}/usr/include/libxml2
4.xml文件放在supporting files 文件下
<?xml version="1.0" encoding="utf-8"?>
<Users>
<User id="001">
<name>Ryan</name>
<age>24</age>
</User>
<User id="002">
<name>Tang</name>
<age>23</age>
</User>
</Users>
5..m文件中写入代码:- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//获取工程目录的xml文件
NSString *filePath = [[NSBundlemainBundle] pathForResource:@"hello"ofType:@"xml"];
NSData *xmlData = [[NSDataalloc] initWithContentsOfFile:filePath];
//使用NSData对象初始化
GDataXMLDocument *doc = [[GDataXMLDocumentalloc] initWithData:xmlData options:0 error:nil];
//获取根节点(Users)
GDataXMLElement *rootElement = [doc rootElement];
//获取根节点下的节点(User)
NSArray *users = [rootElement elementsForName:@"User"];
for (GDataXMLElement *userin users) {
//User节点的id属性
NSString *userId = [[userattributeForName:@"id"]stringValue];
NSLog(@"User id is:%@",userId);
//获取name节点的值
GDataXMLElement *nameElement = [[userelementsForName:@"name"]objectAtIndex:0];
NSString *name = [nameElement stringValue];
NSLog(@"User name is:%@",name);
//获取age节点的值
GDataXMLElement *ageElement = [[userelementsForName:@"age"]objectAtIndex:0];
NSString *age = [ageElement stringValue];
NSLog(@"User age is:%@",age);
NSLog(@"-------------------");
}
}