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

voidremoveUndesiredContent(Node node)
remove Undesired Content
if (node.getLocalName() != null) {
    String localName = node.getLocalName();
voidremoveUserData(Node node, String key)
remove User Data
node.setUserData(key, null, null);
Node child = node.getFirstChild();
while (child != null) {
    removeUserData(child, key);
    child = child.getNextSibling();
voidremoveValue(NamedNodeMap values, String name)
remove Value
if (values.getNamedItem(name) != null) {
    values.removeNamedItem(name);
voidremoveWhitespace(Node node, boolean deep)
Remove any whitespace text nodes from the DOM.
NodeList children = node.getChildNodes();
int length = children.getLength();
for (int i = 0; i < length; i++) {
    Node child = children.item(i);
    if (child.getNodeType() == Node.TEXT_NODE && length > 1) {
        Node previous = child.getPreviousSibling();
        Node next = child.getNextSibling();
        if ((previous == null || previous.getNodeType() == Node.ELEMENT_NODE
...
voidremoveWhitespace(Node parent)
remove Whitespace
parent.normalize();
NodeList nodes = parent.getChildNodes();
if (nodes != null) {
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            removeWhitespace((Element) node);
        } else if (node.getNodeType() == Node.TEXT_NODE) {
...
voidremoveWhitespaceNodes(Node e)
remove Whitespace Nodes
NodeList kids = e.getChildNodes();
int i = 0;
while (i < kids.getLength()) {
    Node kid = kids.item(i);
    if (kid.getNodeType() == Node.TEXT_NODE && ((Text) kid).getTextContent().trim().isEmpty()) {
        e.removeChild(kid);
    } else {
        if (kid.getNodeType() == Node.ELEMENT_NODE) {
...
voidremoveWhitespaceNodes(org.w3c.dom.Element e)
remove Whitespace Nodes
org.w3c.dom.NodeList children = e.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--) {
    org.w3c.dom.Node child = children.item(i);
    if ((child instanceof org.w3c.dom.Text)
            && (((org.w3c.dom.Text) child).getData().trim().length() == 0)) {
        e.removeChild(child);
    } else if (child instanceof org.w3c.dom.Element) {
        removeWhitespaceNodes((org.w3c.dom.Element) child);
...
voidremoveWhitespaceTextNodes(Element parent)
Traverserses through a DOM tree starting at the given element and removes all those text-nodes from it that only contain whitespaces.
final NodeList nl = parent.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    final Node child = nl.item(i);
    if (child.getNodeType() == Node.TEXT_NODE) {
        if (child.getNodeValue().trim().length() == 0) {
            parent.removeChild(child);
            i--; 
    } else if (child.getNodeType() == Node.ELEMENT_NODE && child.getChildNodes().getLength() > 0) {
        removeWhitespaceTextNodes((Element) child);