Java tutorial
//package com.java2s; //License from project: Apache License import org.w3c.dom.*; import javax.annotation.Nonnull; public class Main { public static boolean hasOnlyTextSiblings(@Nonnull Node node) { Node leftSibling = node.getPreviousSibling(); while (leftSibling != null) { if (!(leftSibling instanceof Text)) { return false; } leftSibling = leftSibling.getPreviousSibling(); } Node rightSibling = node.getNextSibling(); while (rightSibling != null) { if (!(rightSibling instanceof Text)) { return false; } rightSibling = rightSibling.getNextSibling(); } return true; } }