Java Utililty Methods XML Node Remove

List of utility methods to do XML Node Remove

Description

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

Method

voidremoveAll(final Node node, final short nodeType, final String name)
Remove named nodes of the specified nodeType from the specified node.
if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
    node.getParentNode().removeChild(node);
} else {
    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        removeAll(list.item(i), nodeType, name);
voidremoveAll(Node node)
remove All
while (node.getChildNodes().getLength() > 0) {
    node.removeChild(node.getFirstChild());
voidremoveAll(Node node, short nodeType, String name)
remove All
if (node == null) {
    return; 
if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
    node.getParentNode().removeChild(node);
} else {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
...
voidremoveAll(Node node, short nodeType, String name)
remove All
if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
    node.getParentNode().removeChild(node);
} else {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        removeAll(list.item(i), nodeType, name);
voidremoveContents(Node parent)
remove Contents
Node node = parent.getFirstChild();
while (node != null) {
    parent.removeChild(node);
    node = node.getNextSibling();
voidremoveElement(Element parent, String tagName)
Remove the node from parent
NodeList nl = parent.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node nd = nl.item(i);
    if (nd.getNodeName().equals(tagName)) {
        parent.removeChild(nd);
voidremoveElements(Node parent, String nature)
Removes all elements with a given nature (as content).
NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        if (checkNature(child, nature)) {
            parent.removeChild(child);
        } else {
            removeElements(child, nature);
...
voidremoveElementXML(Node node, short nodeType, String name)
remove Element XML
if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
    node.getParentNode().removeChild(node);
} else {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        removeElementXML(list.item(i), nodeType, name);
voidremoveEmptyHeadings(Node root)
remove Empty Headings
NodeList hNodes = ((Element) root).getElementsByTagName("text:h");
for (int i = 0; i < hNodes.getLength(); i++) {
    Node node = hNodes.item(i);
    if (node.getChildNodes().getLength() > 0) {
        boolean empty = true;
        for (int j = 0; j < node.getChildNodes().getLength(); j++) {
            if (!node.getChildNodes().item(j).getTextContent().trim().equals("")) {
                empty = false;
...
voidremoveEmptyHeadings(Node root)
Remove empty text:h elements.
NodeList hNodes = ((Element) root).getElementsByTagName("text:h");
for (int i = 0; i < hNodes.getLength(); i++) {
    Node node = hNodes.item(i);
    if (node.getChildNodes().getLength() > 0) {
        boolean empty = true;
        for (int j = 0; j < node.getChildNodes().getLength(); j++) {
            if (!node.getChildNodes().item(j).getTextContent().trim().equals("")) {
                empty = false;
...