Java Utililty Methods XML Element Remove

List of utility methods to do XML Element Remove

Description

The list of methods to do XML Element Remove are organized into topic(s).

Method

voidremove(Element element, Predicate shouldRemovePredicate)
Removes child elements that meet a certain condition.
NodeList childNodes = element.getChildNodes();
int i = 0;
while (i < childNodes.getLength()) {
    Node currentNode = childNodes.item(i);
    Element child = (currentNode instanceof Element) ? ((Element) currentNode) : null;
    if (child != null && shouldRemovePredicate.test(child)) {
        element.removeChild(currentNode);
    } else {
...
voidremoveAll(Element node, String subElement)
remove All
for (Element e : findAll(node, subElement))
    node.removeChild(e);
voidremoveContent(Element element)
remove Content
NodeList nodeList = element.getChildNodes();
while (nodeList.getLength() > 0) {
    element.removeChild(nodeList.item(0));
voidremoveElements(Element from, String named)
Removes all children named named from element from
NodeList children = from.getChildNodes();
boolean removeNextNewline = false;
List<Node> removes = new ArrayList<Node>();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if ((child != null) && named.equals(child.getNodeName())) {
        removes.add(child);
        removeNextNewline = true;
...
NoderemoveEmptyElements(Node node)
Takes the supplied node and recursively removes empty (no content/child nodes) elements
NodeList list = selectNodeList(node, "*[string-length(normalize-space(.)) = 0]");
for (int i = 0; i < list.getLength(); i++) {
    node.removeChild(list.item(i));
if (node.hasChildNodes()) {
    NodeList childs = node.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        if (childs.item(i) instanceof Element) {
...
voidremovePreviousSiblingText(Element element)
Removes any previous siblings text nodes
while (true) {
    Node sibling = element.getPreviousSibling();
    if (sibling instanceof Text) {
        detach(sibling);
    } else {
        break;
voidremoveSchemaLocation(Element el)
remove Schema Location
NamedNodeMap children;
Node node;
Attr attribute;
String xsi = null;
children = el.getAttributes();
for (int i = 0; i < children.getLength(); i++) {
    node = children.item(i);
    if (node instanceof Attr) {
...
voidremoveTopPi(Element e, Element pi)
Remove the Pi on top of e
Element piContainer = (Element) pi.getParentNode();
piContainer.replaceChild(e, pi);
voidremoveWhitespaceNodes(Element e)
Removes whitespace from element content.
NodeList children = e.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--) {
    Node child = children.item(i);
    if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
        e.removeChild(child);
    } else if (child instanceof Element)
        removeWhitespaceNodes((Element) child);
voidremoveWhitespaceNodes(Element e)
Helper function that removes whitespace nodes from Element objects to allow for easier parsing
NodeList children = e.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--) {
    Node child = children.item(i);
    if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
        e.removeChild(child);
    } else if (child instanceof Element) {
        removeWhitespaceNodes((Element) child);