* instances of a Text node next to another Text node are created.
*
* @param elem The element to remove from the document.
*/
public static void removeElementPreservingText(Element elem) {
ParentNode parent = elem.getParent();
/* Put fingers on relevant nodes */
Node previous = XOMTools.getPreviousSibling(elem);
Node next = XOMTools.getNextSibling(elem);
int index = elem.getParent().indexOf(elem);
if(elem.getChildCount() > 0) {
/* Put fingers on relevant inner nodes */
Node contentsFirst = elem.getChild(0);
Node contentsLast = elem.getChild(elem.getChildCount()-1);
/* Transfer inner nodes to parent */
while(elem.getChildCount() > 0) {
Node nn = elem.getChild(0);
nn.detach();
parent.insertChild(nn, index);
index++;
}
/* Perform Text surgery */
if((previous instanceof Text) && (contentsFirst instanceof Text)) {
((Text)previous).setValue(previous.getValue() + contentsFirst.getValue());