TBXMLiOS的輕量級XML解析庫
TBXML是一個用于iOS上的解析速度非??斓妮p量級XML解析庫。提供了非常簡潔的接口,使用起來很簡單。
它提供了可以通過文件路徑、URL、XML文件內容、內容字符串等方式載入XML文件,提供了獲取XML節(jié)點和屬性值的方法,以及一個遍歷節(jié)點的方法。不過TBXML只提供了讀的功能。
初始化和釋放TBXML
TBXML* tbxml =[ [[TBXML alloc] initWithXMLFile:@"file.xml"] retain];
一定要在后面加上retain,要不運行app時,會出現(xiàn)非法訪問的錯誤。
使用完畢后,注意釋放:
[tbxml release];
使用遞歸方法遍歷所有節(jié)點和屬性的例子
- (void) traverseElement:(TBXMLElement *)element {
do {
// 顯示XML元素名稱
NSLog(@"%@",[TBXML elementName:element]);
// 獲取到當前節(jié)點的第一個屬性
TBXMLAttribute * attribute = element->firstAttribute;
// if attribute is valid
while (attribute) {
// 在log窗口中顯示屬性的名稱和值
NSLog(@"%@->%@ = %@",[TBXML elementName:element],[TBXML attributeName:attribute], [TBXML attributeValue:attribute]);
// 獲取下一個屬性
attribute = attribute->next;
}
// 遞歸遍歷下一個子元素
if (element->firstChild) [self traverseElement:element->firstChild];
// 獲取同級元素
} while ((element = element->nextSibling));
}
