I have a scenario where I need to get the sub pages inside a book (max 3) and move those pages to parent of book.
Like:
<book>
<book1>
<page1>
<page2>
<page3>
<page4>
</book1>
</book>
So, if number of pages under book1 are greater than 3, split the book1 into 2 namely book1_h_1 and book2_h_2 and move page1, page2, page under book1_h_1 and move page4 under book1_h_2. There can be a scenario of more than 4 pages under book1 like 10 or more.
In order to get how many new books needs to be created I did the following
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));
}
}
My Question is how can I add list "batches" to newly created books "book1_h_1 and book1_h_2?
Can someone help me on this?