-1

我有以下xml。我正在尝试使用 gdataxml 解析器来解析它。我能够解析 xml 的第一部分<myBean>to </myBean>,但从第二部分 from 开始,随着内部递归<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>到 < myBean>。如何检查 a 是否<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>

4

2 回答 2

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"]];
         }

希望这会有所帮助。

于 2012-04-14T06:03:32.130 回答
0

如何使用 GDataXML 读写 XML 文档

于 2012-03-30T09:24:51.973 回答