我有一个包含国家和代码的XML文件,我已经将这个XML文件放在Demo.xml中。我的问题是只有第一个字典被保存了。或者有人可以告诉我其他解析方法,比如使用NSXML或GData。
这是 XML 的链接:https://www.dropbox.com/s/x4zpxgi42h0rllz/Demo.xml。
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"Demo" ofType:@"xml"];
NSData *data=[NSData dataWithContentsOfFile:filePath];
addArray=[[NSMutableArray alloc]init];
dictData=[[NSMutableDictionary alloc]init];
TBXML *tbxml = [TBXML tbxmlWithXMLData: data];
TBXMLElement *rootXMLElement=tbxml.rootXMLElement;
TBXMLElement *rootElemnt=[TBXML childElementNamed:@"root" parentElement:rootXMLElement];
while (rootElemnt!=nil) {
TBXMLElement *item = [TBXML childElementNamed:@"item" parentElement:rootElemnt];
if (item!=nil) {
TBXMLElement *country = [TBXML childElementNamed:@"country" parentElement:item];
TBXMLElement *code = [TBXML childElementNamed:@"code" parentElement:item];
dictData=[NSMutableDictionary dictionaryWithObjectsAndKeys:[TBXML textForElement:country],@"country",[TBXML textForElement:code],@"code",nil];
rootElemnt = [TBXML nextSiblingNamed:@"item" searchFromElement:rootElemnt];
[addArray addObject:dictData];
}
}
NSLog(@"====%@",addArray);
输出结果如下:
====(
{
code = 355;
country = Albania;
}
)
这是 XML 的链接:https://www.dropbox.com/s/x4zpxgi42h0rllz/Demo.xml。