Example usage for org.w3c.dom Node getPreviousSibling

List of usage examples for org.w3c.dom Node getPreviousSibling

Introduction

In this page you can find the example usage for org.w3c.dom Node getPreviousSibling.

Prototype

public Node getPreviousSibling();

Source Link

Document

The node immediately preceding this node.

Usage

From source file:Main.java

/**
 * Find the previous sibling element./* w w  w.ja v  a2  s  .  co m*/
 * 
 * @param startNode XML start node.
 * @return the previous sibling element.
 */
public static Element findPreviousSibling(Node startNode) {
    if (startNode == null)
        return null;

    while (startNode != null) {
        startNode = startNode.getPreviousSibling();
        if (startNode == null)
            return null;
        if (startNode.getNodeType() == Node.ELEMENT_NODE)
            return (Element) startNode;
    }

    return null;
}

From source file:Main.java

/**
 * Returns the occurrences of a given node/element. Checks whether the previous/following
 * elements have the same name.//from   w  w w  .  j ava  2s  .  c o m
 * 
 * @param node The node whose occurrences should be determined.
 * @return      The occurrences of node. Greater than or equal to 1.
 */
public static int getOccurs(Node node) {
    int occurs = 1;
    String nodeName = node.getNodeName();

    Node prevSib = node.getPreviousSibling();
    while (prevSib != null) {
        if (prevSib.getNodeName().compareTo(nodeName) == 0 && prevSib.getNodeType() == Node.ELEMENT_NODE) {
            occurs++;
            prevSib = prevSib.getPreviousSibling();
        } else {
            prevSib = null;
        }
    }

    Node nextSib = node.getNextSibling();
    while (nextSib != null) {
        if (nextSib.getNodeName().compareTo(nodeName) == 0 && nextSib.getNodeType() == Node.ELEMENT_NODE) {
            occurs++;
            nextSib = nextSib.getNextSibling();
        } else {
            nextSib = null;
        }
    }

    return occurs;
}

From source file:Main.java

/**
 * Remove any whitespace text nodes from the DOM. Calling this before saving
 * a formatted document will fix the formatting indentation of elements
 * loaded from a different document.//from   w  w  w.  java  2  s.  c o m
 * 
 * @param node
 *            Node to remove whitespace nodes from
 * @param deep
 *            Should this method recurse into the node's children?
 */
public static void removeWhitespace(Node node, boolean deep) {
    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
                    || previous.getNodeType() == Node.COMMENT_NODE)
                    && (next == null || next.getNodeType() == Node.ELEMENT_NODE
                            || next.getNodeType() == Node.COMMENT_NODE)) {
                String content = child.getTextContent();
                if (content.matches("\\s*")) //$NON-NLS-1$
                {
                    node.removeChild(child);
                    i--;
                    length--;
                }
            }
        } else if (deep && child.getNodeType() == Node.ELEMENT_NODE) {
            removeWhitespace(child, deep);
        }
    }
}

From source file:Main.java

/**
 * Determines the index (starting at 0) of the given element relative to
 * other element nodes for the same parent.
 *///  w w  w .  jav  a  2  s.  c om
public static int getElementPosition(Element element) {
    int num = -1;
    Node node = element;
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            ++num;
        }
        node = node.getPreviousSibling();
    }
    return num;
}

From source file:Main.java

/**
 * Get the previous sibling element of the specified element, null if it has no previous other siblings.
 * /*from  w  ww .  j  a  va 2s.c  o  m*/
 * @param element The element to search siblings for.
 * @return The previous sibling element, null if it has none.
 */
public static Element getPrevSibling(Element element) {
    if (element == null)
        return null;
    Node node = element.getPreviousSibling();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
        node = node.getPreviousSibling();
    }
    return null;
}

From source file:Main.java

/** Finds and returns the last child element node. 
 *  Overload previous method for non-Xerces node impl.
 */// w w  w .  ja  v a 2s .co m
public static Element getLastChildElement(Node parent) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static int getCurrentPosition(final Node refNode) {
    if (refNode == null) {
        return -1;
    }//  w w  w . j a  va 2  s. c om

    int counter = 0;
    Node current = refNode;

    while (current != null) {
        if (current.getNodeType() == Node.ELEMENT_NODE) {
            counter++;
        }

        current = current.getPreviousSibling();
    }

    return counter;
}

From source file:Main.java

/** Finds and returns the last child element node. */
public static Element getLastChildElement(Node parent) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }/*  w w w.j  a v a2s . c o  m*/
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static int getCurrentPosition(Node refNode) {
    if (refNode == null) {
        return -1;
    }//from w  w w . j a  v  a2s .  c o m

    int counter = 0;
    Node current = refNode;

    while (current != null) {
        if (current.getNodeType() == Node.ELEMENT_NODE) {
            counter++;
        }

        current = current.getPreviousSibling();
    }

    return counter;
}

From source file:Main.java

private static String getXPath(Node node, String xpath) {
    String elementName = "";
    if (node instanceof Element) {
        elementName = node.getNodeName();
        int prev_siblings = 1;
        Node prev_sibling = node.getPreviousSibling();
        while (null != prev_sibling) {
            if (prev_sibling.getNodeType() == node.getNodeType()) {
                if (prev_sibling.getNodeName().equalsIgnoreCase(node.getNodeName())) {
                    prev_siblings++;/*from  w  w w  . j  a  v  a2 s.  c o m*/
                }
            }
            prev_sibling = prev_sibling.getPreviousSibling();
        }
        elementName = elementName.concat("[" + prev_siblings + "]");
    }
    Node parent = node.getParentNode();
    if (parent == null) {
        return xpath;
    }
    return getXPath(parent, "/" + elementName + xpath);
}