Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取表的索引值?_Java_Xml_Docx4j - Fatal编程技术网

Java 如何获取表的索引值?

Java 如何获取表的索引值?,java,xml,docx4j,Java,Xml,Docx4j,我尝试使用以下方法获取表的索引值 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

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

 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();
    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;

   }
int tIndex=getIndex(ca.getContent(),tbl); 如果(tIndex!=98761){ //你想干什么就干什么 } 私有静态int getIndex(列表,对象bm) { 用于(对象框:列表) { if(XmlUtils.unwrap(ox).等于(bm)) { int o=列表索引ox; 返回o; } } 返回98761; }
在我的情况下,tIndex不会触及那么多num(98761)。如果您觉得不安全,只需增加返回值