在Iphone中解析Xml并向NSMutableArray添加值
问题描述:
我是xml解析的新手。我有以下xml在Iphone中解析Xml并向NSMutableArray添加值
<myMainList>
<mySubList>
<edited>true</edited>
<mySharedNumber>W59QYBZKJ4</mySharedNumber>
</mySubList>
<mySubList>
<edited>false</edited>
<mySharedNumber>TOW4KLP9WD</mySharedNumber>
</mySubList>
<mySubList>
<edited>true</edited>
<mySharedNumber>XH8JDIZA64</mySharedNumber>
</mySubList>
<mySubList>
<edited>false</edited>
<mySharedNumber>V2YOHSNODT</mySharedNumber>
</mySubList>
</myMainList>
我编辑了我的问题。
我不熟悉循环遍历整个XML并将值添加到我的数组中。有人可以告诉我如何将4 sharedNumberList
值添加到我的阵列中。
编辑:
GDataXMLElement *node;
答
NSArray * array = [node nodesForXPath:@"//return/myMainList/mySubList" error:nil];
NSLog(@"count :%d",[array count]);
int sharedContacts = [array count];
NSMutableArray *mySharedListArray = [[NSMutableArray alloc]init];
for(int i = 1; i<= sharedContacts; i++){
NSString *xmlDataFetcher = [NSString stringWithFormat:@"//return/myMainList/mySubList[%d]",i ];
NSString *parsedNumbers = [node nodeStringForXPath:[xmlDataFetcher stringByAppendingString:@"/mySharedNumber"]];
NSString *parsedEdit = [node nodeStringForXPath:[xmlDataFetcher stringByAppendingString:@"/edited"]];
NSLog(@"Parsed Edited %@", [node nodeStringForXPath:[xmlDataFetcher stringByAppendingString:@"/edited"]]);
NSLog(@"Parsed sharedNumber %@", [node nodeStringForXPath:[xmlDataFetcher stringByAppendingString:@"/mySharedNumber"]]);
NSString *arrayEntry = [NSString stringWithFormat:@"%@%@", parsedNumbers, parsedEdit];;
[mySharedListArray addObject:arrayEntry];
}
NSLog(@"Array entry %@", mySharedListArray);
我已经加了很多NSLog
在答案中,以便您可以随心所欲地登录
答
我没有这样做最有可能,这是从返回XPath查询阵列,使得试试这个
[mySharedListArray addObjectsFromArray:[node nodesForXPath:@"//return/MyList" error:nil]];
它显示'不兼容的指针类型发送'N SString *'参数的类型'NSArray *'' – 2012-03-17 07:18:47
@JeanPaulScott请看看我的编辑...这一定会工作。 – 2012-03-17 07:39:41
'nodesForXPath'方法也有一个'NSError **'参数。 – 2012-03-17 07:47:15