我有以下xml。我试图使用gdataxml解析器解析这个。我能够将xml的第一部分<myBean>
解析为</myBean>
,但是从第二部分<trackBean>
解析,trackBean
正在递归时变得越来越难内。我发现很难为此开发逻辑。我被困在这里第二天。
<return>
<myTrackBean>
<myBean>
<myStatus>false</myStatus>
<hisStatus>false</hisStatus>
<secondVcardBean>
<contactId>108</contactId>
<myNumber>KXUOYHGCIO</myNumber>
<userId>56</userId>
<version>1</version>
</secondVcardBean>
<myNumber>KXUOYHGCIO</myNumber>
</myBean>
<trackBean>
<myBean>
<myStatus>false</myStatus>
<hisStatus>false</hisStatus>
<secondVcardBean>
<contactId>105</contactId>
<myNumber>5D1X7XP6CW</myNumber>
<userId>54</userId>
<version>1</version>
</secondVcardBean>
<myNumber>5D1X7XP6CW</myNumber>
</myBean>
<trackBean>
<myBean>
<myStatus>false</myStatus>
<hisStatus>false</hisStatus>
<secondVcardBean>
<contactId>103</contactId>
<myNumber>0C3RM5UKBB</myNumber>
<userId>53</userId>
<version>8</version>
</secondVcardBean>
<myNumber>0C3RM5UKBB</myNumber>
</myBean>
</trackBean>
</trackBean>
</myTrackBean>
</return>
以下是我用于解析第一部分的代码,从第一个<myBean>
到&lt; myBean>
。如何检查其父<trackBean>
<trackBean>
NSArray * array = [node nodesForXPath:@"//return/myTrackBean" error:nil];
int noOfmyTrackBean = [array count];
for(int i = 1; i<= noOfmyTrackBean; i++){
NSString *tempXmlData = [NSString stringWithFormat:@"//return/myTrackBean/myBean"];
NSString *myStatus = [node nodeStringForXPath:[tempXmlData stringByAppendingString:@"/myStatus"]];
NSString *hisStatus = [node nodeStringForXPath:[tempXmlData stringByAppendingString:@"/hisStatus"]];
NSString *contactId = [node nodeStringForXPath:[tempXmlData stringByAppendingString:@"/secondVcardBean/contactId"]];
NSString *myNumber = [node nodeStringForXPath:[tempXmlData stringByAppendingString:@"/secondVcardBean/myNumber"]];
我理解,难点在于<trackBean>
<trackBean>
子节点
答案 0 :(得分:1)
我认为你不需要担心递归,因为gdataxml似乎在内部处理那部分。您可以正常解析它,因为解析器对象获取在其自身内重复的节点的计数。我不知道它究竟是你需要什么,但它肯定会帮助你。
NSArray * array = [node nodesForXPath:@"//return/myTrackBean" error:nil];
NSLog(@"count :%d",[array count]);
NSLog(@"Node :%@",node);
int noOfPrimaryNodes = [array count];
for(int i = 1; i<= noOfPrimaryNodes; i++){
//For use from the second node
NSString *myxmlData = [NSString stringWithFormat:@"//return/myTrackBean/trackBean"];
NSLog(@"myxmlData :%@",myxmlData);
NSArray *array = [node nodesForXPath:@"//return/myTrackBean/trackBean" error:nil];
NSLog(@"Array count %d", [array count]);
if([array count]>0){
GDataXMLElement *myNode = [[node nodesForXPath:@"//return/myTrackBean/trackBean" error:&error]objectAtIndex:0];
NSLog(@"myNode :%@",myNode);
NSString *changingNode = @"//trackBean";
for(int j = 0; j < [array count]; j++){
NSString *myStatus = [node nodeStringForXPath:[changingNode stringByAppendingString:@"/myBean/myStatus"]];
NSString * hisStatus = [node nodeStringForXPath:[changingNode stringByAppendingString:@"/myBean/hisStatus"]];
NSString *contactId = [node nodeStringForXPath:[changingNode stringByAppendingString:@"/myBean/secondVcardBean/contactId"]];
NSString *myNumber = [node nodeStringForXPath:[changingNode stringByAppendingString:@"/myBean/secondVcardBean/myNumber"]];
NSString *versionNumber = [node nodeStringForXPath:[changingNode stringByAppendingString:@"/myBean/secondVcardBean/versionNumber"]];
}
希望这会有所帮助。
答案 1 :(得分:0)