List 添加列表<;元素>;到JDOM元素

List 添加列表<;元素>;到JDOM元素,list,element,jdom,List,Element,Jdom,我有一个场景,我需要获取一本书中的子页面(最多3个),然后将这些页面移动到书的父页面 比如: 因此,如果book1下的页数大于3,则将book1拆分为2,即book1_h_1和book2_h_2,并将page1、page2、page1_h_1下的页面和page4移动到book1_h_2下。book1下可能有超过4页的场景,比如10页或更多 为了得到需要创作多少本新书,我做了以下工作 List<Element> elChildren = book1.getChildren(); i

我有一个场景,我需要获取一本书中的子页面(最多3个),然后将这些页面移动到书的父页面

比如:


因此,如果book1下的页数大于3,则将book1拆分为2,即book1_h_1和book2_h_2,并将page1、page2、page1_h_1下的页面和page4移动到book1_h_2下。book1下可能有超过4页的场景,比如10页或更多

为了得到需要创作多少本新书,我做了以下工作

List<Element> elChildren = book1.getChildren();
int size = 3;
int numBatches = (elChildren.size() / size) + 1;
List<Element> batches = null;
for (int index = 0; index < numBatches; index++) {
int count = index + 1;
int fromIndex = Math.max(((count - 1) * size), 0);
int toIndex = Math.min((count * size), elChildren.size());
batches = elChildren.subList(fromIndex, toIndex);
}

public static void createElementNew(Element parent, int i){
for(int q = 1; q <=i; q++){
parent.addContent(new Element("book1_h_"+q));
}
}
List elChildren=book1.getChildren();
int size=3;
int numBatches=(elChildren.size()/size)+1;
列表批次=空;
for(int index=0;index对于(intq=1;q您可以使用一个迭代器完成全部工作……无需将内容保存在变量和存储桶列表中

“booke”将指向book1元素

Element booke = ..... ;
Element parent = booke.getParentElement();
int bookpos = parent.indexOf(booke);
Iterator<Element> lit = booke.getChildren().iterator();

int cnt = 0;
int bkh = 1;
Element bke = null;
String namebase = booke.getName() + "_h_";
while (it.hasNext()) {
    Element page = it.next();
    if (cnt >= 3) {
       if ((cnt % 3) == 0) {
          bkh++;
          bookpos++;
          bke = new Element(namebase + bkh);
          parent.addContent(bookpos, bke);
       }
       it.remove();
       bke.add(page);
    }
    cnt++;
}
if (cnt > 3) {
    booke.setName(namebase + "1");
}
元素booke=;
Element parent=booke.getParentElement();
int bookpos=parent.indexOf(booke);
迭代器lit=book.getChildren().Iterator();
int-cnt=0;
int-bkh=1;
元素bke=null;
字符串namebase=booke.getName()+“_h_u2;”;
while(it.hasNext()){
元素页=it.next();
如果(cnt>=3){
如果((cnt%3)=0){
bkh++;
bookpos++;
bke=新元素(名称库+bkh);
父.addContent(bookpos,bke);
}
it.remove();
bke.添加(第页);
}
cnt++;
}
如果(cnt>3){
booke.setName(名称库+“1”);
}

Rolfl

嗨,Rolfl,你能帮我解决这个问题吗?
Element booke = ..... ;
Element parent = booke.getParentElement();
int bookpos = parent.indexOf(booke);
Iterator<Element> lit = booke.getChildren().iterator();

int cnt = 0;
int bkh = 1;
Element bke = null;
String namebase = booke.getName() + "_h_";
while (it.hasNext()) {
    Element page = it.next();
    if (cnt >= 3) {
       if ((cnt % 3) == 0) {
          bkh++;
          bookpos++;
          bke = new Element(namebase + bkh);
          parent.addContent(bookpos, bke);
       }
       it.remove();
       bke.add(page);
    }
    cnt++;
}
if (cnt > 3) {
    booke.setName(namebase + "1");
}