Java Docx4j:在父项下当前项后插入项X次

Java Docx4j:在父项下当前项后插入项X次,java,insert,ms-word,docx4j,Java,Insert,Ms Word,Docx4j,我正在尝试使用DOCX4J解析内容并将其插入模板。作为这个模板的一部分,我有循环,我需要复制两个标记之间的所有内容,并将所有内容重复X次 相关代码如下: public List<Object> getBetweenLoop(String name){ String startTag = this.tag_start + name + "_LOOP" + this.tag_end; String endTag = this.tag_start + nam

我正在尝试使用DOCX4J解析内容并将其插入模板。作为这个模板的一部分,我有循环,我需要复制两个标记之间的所有内容,并将所有内容重复X次

相关代码如下:

public List<Object> getBetweenLoop(String name){
        String startTag = this.tag_start + name + "_LOOP" + this.tag_end;
        String endTag = this.tag_start + name + this.tag_end;
        P begin_loop = this.getTagParagraph(startTag);
        P end_loop = this.getTagParagraph(endTag);

        ContentAccessor parent = (ContentAccessor) this.getCommonParent(begin_loop, end_loop);

        List<Object> loop = new ArrayList<Object>();

        boolean save = false;
        //Cycle through the content for the parent and copy all the objects that
        //are between and including the start and end-tags
        for(Object item : parent.getContent()){
            if(item.equals(begin_loop) || item.equals(end_loop))
                save = (save) ? false : true;
            if(save || item.equals(end_loop)){
                loop.add(XmlUtils.deepCopy(item));
            }
            if(item.equals(end_loop)){
                //Here I want to insert everything copied X times after the current item and then exit the for loop.
                //This is the part I'm not sure how to do since I don't see any methods "Insert Child", etc.
            }
        }
        return loop;
    }
公共列表GetBetweenLop(字符串名称){
字符串startTag=this.tag\u start+name+“\u LOOP”+this.tag\u end;
String endTag=this.tag\u start+name+this.tag\u end;
P begin\u loop=this.gettagparagration(startTag);
P end_loop=this.gettagparagration(endTag);
ContentAccessor parent=(ContentAccessor)this.getCommonParent(开始\u循环,结束\u循环);
列表循环=新建ArrayList();
布尔保存=假;
//循环浏览父对象的内容,并复制所需的所有对象
//介于并包括开始标记和结束标记之间
对于(对象项:parent.getContent()){
if(item.equals(开始循环)| | item.equals(结束循环))
保存=(保存)?false:true;
if(保存| |项.equals(结束|循环)){
loop.add(XmlUtils.deepCopy(item));
}
if(项等于(结束循环)){
//在这里,我想在当前项之后插入复制X次的所有内容,然后退出for循环。
//这是我不知道该怎么做的部分,因为我没有看到任何方法“insertchild”,等等。
}
}
回路;
}
GetTagParague成功返回表示所发送标记的段落的对象。这个很好用

getCommonParent返回两个提供的标记之间的共享父级。这个很好用


正如注释所述,我的问题是如何将新复制的项目插入到适当的位置。

如果您希望插入存储在
循环
集合中的所有对象,您只需执行以下操作(在注释的条件中):

表示
end\u循环
对象(段落或任何内容),并将收集到的所有对象插入到
循环
集合中。(
addAll
可能也需要一个int参数,我想不起来了,但如果需要,那就是整个
MainDocumentPart.getContent()
JAXB文档表示中所需的索引)。

@Ben,谢谢

如果你知道任何情况下,下面不会工作,请让我知道

事实上,我刚刚发现了一些非常相似的东西,但最终改变了更多的代码。下面是我总结的内容

   public void repeatLoop(String startTag, String endTag, Integer iterations){
        P begin_loop = this.getTagParagraph(startTag);
        P end_loop = this.getTagParagraph(endTag);
        ContentAccessor parent = (ContentAccessor) this.getCommonParent(begin_loop, end_loop);
        List<Object> content = parent.getContent();

        Integer begin_pointer = content.indexOf(begin_loop);
        Integer end_pointer = content.indexOf(end_loop);

        List<Object> loop = new ArrayList<Object>();
        for(int x=begin_pointer; x <= end_pointer; x = x + 1){
            loop.add(XmlUtils.deepCopy(content.get(x)));
        }

        Integer insert = end_pointer + 1;
        for(int z = 1; z < iterations; z = z + 1){
            content.addAll(insert, loop);
            insert = insert + loop.size();
        }
    }
public void repeatLoop(字符串开始标记、字符串结束标记、整数迭代){
P begin\u loop=this.gettagparagration(startTag);
P end_loop=this.gettagparagration(endTag);
ContentAccessor parent=(ContentAccessor)this.getCommonParent(开始\u循环,结束\u循环);
List content=parent.getContent();
整数begin\u pointer=content.indexOf(begin\u循环);
整数end\u指针=content.indexOf(end\u循环);
列表循环=新建ArrayList();
对于(int x=开始指针;x
   public void repeatLoop(String startTag, String endTag, Integer iterations){
        P begin_loop = this.getTagParagraph(startTag);
        P end_loop = this.getTagParagraph(endTag);
        ContentAccessor parent = (ContentAccessor) this.getCommonParent(begin_loop, end_loop);
        List<Object> content = parent.getContent();

        Integer begin_pointer = content.indexOf(begin_loop);
        Integer end_pointer = content.indexOf(end_loop);

        List<Object> loop = new ArrayList<Object>();
        for(int x=begin_pointer; x <= end_pointer; x = x + 1){
            loop.add(XmlUtils.deepCopy(content.get(x)));
        }

        Integer insert = end_pointer + 1;
        for(int z = 1; z < iterations; z = z + 1){
            content.addAll(insert, loop);
            insert = insert + loop.size();
        }
    }