【问题标题】:Selecting nodes with XPath使用 XPath 选择节点
【发布时间】:2013-11-28 09:05:39
【问题描述】:

我正在尝试使用 XPath 选择节点... 我正在使用以下代码是我的 iOS 应用程序来收集有关我拥有的书籍类型的一些信息,无论它们是平装本还是精装本:

nodes= [rootNode nodesForXpath:@"Collection/books" error:nil];
for (DDXMLNode* node in nodes)
{
    Booktype* bt = [[Booktype alloc] init];
    DDXMLNode *nameNode = [[node nodesForXpath:@"OfType" error:nil]; objectAtIndex:0];
    bt.type = [nameNode stringValue];

   // And lastly, I am adding this object to my array that will be the datasource for my tableView
   [array addObject:bt];
}

我的库 XML 如下所示:

<Collection>

<books>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
  <ofType>Hardcover</ofType>
</books>

<books>
  <title lang="eng">Stella Bain</title>
  <price>19.99</price>
  <ofType>Hardcover</ofType>
</books>

<books>
  <title lang="eng">The First Phone Call from Heaven</title>
  <price>12.95</price>
  <ofType>Paperback</ofType>
</books>

<books>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
  <ofType>Paperback</ofType>
</books>

</Collection>

所以我有两本平装书和两本精装书:太好了。现在的问题是,当将数据加载到我的tableView 时,我的ofType 请求总共得到了 4 个列表:

我得到一个看起来像这样的表格视图:

我怎样才能只有 1 个该类型的实例? 因此,我将只获得 1 个平装清单和 1 个精装清单,而不是每个实例 2 个...我的意图是稍后添加另一个tableView,它将列出所选书籍类别中的所有书籍。

请在您的回答中尽可能具体和详细。

问候, -VZM

更新:我已尝试实现以下内容:

if (![array containsObject:bt]) {
    [array addObject:bt];
}

但不幸的是,这会返回相同的结果。

【问题讨论】:

    标签: ios objective-c xml xpath nodes


    【解决方案1】:

    您可以在将Booktype 添加到array 之前简单地检查一下,

    if (![array containsObject:bt]) {
        [array addObject:bt];
    }
    

    【讨论】:

    • 我刚刚尝试实现此代码,但不幸的是它不起作用......我得到的结果与我发布问题时得到的结果相同@Anusha
    【解决方案2】:

    您需要为此使用NSPredicate

    变化:

    [array addObject:bt];
    

    与:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.type == %@", bt.type];
    if ([[array filteredArrayUsingPredicate:predicate] count] == 0)
    {
        [array addObject:bt];
    }
    

    【讨论】:

      【解决方案3】:

      我希望这会给你一个想法......

        NSMutableArray *arrayPaperCover = [[NSMutableArray alloc]init];
          NSMutableArray *arrayHardCover = [[NSMutableArray alloc]init];
      
          nodes= [rootNode nodesForXpath:@"Collection/books" error:nil];
          for (DDXMLNode* node in nodes)
          {
              Booktype* bt = [[Booktype alloc] init];
              DDXMLNode *nameNode = [[node nodesForXpath:@"OfType" error:nil] objectAtIndex:0];
              bt.type = [nameNode stringValue];
      
      
              if ([bt.type isEqualToString:@"Paperback"]) {
                  [arrayPaperCover addObject:bt];
      
              }
              else ([bt.type isEqualToString:@"Hardcover"]) {
                  [arrayHardCover addObject:bt];
      
              }
      
          }
          NSMutableArray * dataSource = [[NSMutableArray alloc]init]; // this will be your data source 
          [dataSource addObject:arrayPaperCover];
          [dataSource addObject:arrayHardCover];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-25
        • 2017-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多