我有一个包含国家和代码的 xml 文件。我已将 xml 放在 Demo.xml 中。我的问题是只保存了第一个字典。或者任何人都可以告诉我其他的解析方式,比如使用 nsxml 或 gdata
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;
}
)