如何获取table的索引值?

How to get the index value of table?

我尝试使用以下方法获取 table 的索引值

 if(tbl.getParent() instanceof Body )
{
  Body body = (Body) tbl.getParent();
  int tIndex = body.getContent().indexOf(tbl); 
  body.getContent().remove(tbl); 
}

另一种方法是使用 contentAccessor

 if(tbl.getParent() instanceof ContentAccessor )
{
  ContentAccessor ca = (ContentAccessor) tbl.getParent();
  int tIndex = ca.getContent().indexOf(tbl);
  ca.getContent().remove(tbl); 
}

但我得到的不是实际索引值,而是 -1 作为 tIndex。它也没有从它的父级中删除 tbl(ca.getContent().remove(tbl);) 。

有没有其他方法可以得到Tbl的索引值?

    ContentAccessor ca = (ContentAccessor) tbl.getParent();
    int tIndex = getIndex(ca.getContent(), tbl);
    if(tIndex != 98761){
     //do whatever you want to
    }

    private static int getIndex(List<Object> theList, Object bm) 
    {
      for (Object ox : theList) 
      {
        if (XmlUtils.unwrap(ox).equals(bm)) 
        {
            int o = theList.indexOf(ox);
            return o;

        }
      }
    return 98761;

   }

在我的例子中,tIndex 不会触及那么多 num(98761)。如果您觉得不安全,请增加 return 值。