KissXML范例

引用
#import "DDXML.h"
#import "DDXMLElementAdditions.h"

//XML File
//<bookcase>
// <book>
// <bookName>My First KissXml Article</bookName>
// <bookAuthor>mOMo</bookAuthor>
// </book>
// <book>
// <bookName>Delete Life</bookName>
// <bookAuthor>Mary</bookAuthor>
// </book>
//</bookcase>

//取得XML檔案實體位置
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"xmlFile" ofType:@"xml"];

//將XML檔案讀出
NSString *xmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

//開始使用KissXML,將讀出的XML字串指定給DDXMLDocument物件
DDXMLDocument *xmlDoc = [[DDXMLDocument alloc]initWithXMLString:xmlString options:0 error:nil];

//釋放
xmlString = nil;

//開始解析
NSArray *children = nil;

//使用XPath取得要走訪的節點
children = [xmlDoc nodesForXPath:@"bookcase/book" error:nil];

//依符合的節點數量走訪
for (int i=0; i < [children count]; i++) {
//建立DDXMLNode
DDXMLNode *child = [children objectAtIndex:i];
NSLog(@"書名:%@", [[child childAtIndex:0] stringValue];);
NSLog(@"作者:%@", [[child childAtIndex:1] stringValue];);
}

//釋放
[xmlDoc release];

你可能感兴趣的:(xml)