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

/** Finds and returns the last child node with the given name. */
public static Element getLastChildElement(Node parent, String elemName) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().equals(elemName)) {
                return (Element) child;
            }//from w  w w.  j a va  2s .  c  o  m
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:importer.handler.post.stages.Discriminator.java

static Element firstSibling(Element elem) {
    Node start = elem;
    while (start.getPreviousSibling() != null)
        start = start.getPreviousSibling();
    while (start.getNodeType() != Node.ELEMENT_NODE)
        start = start.getNextSibling();//  w  ww.jav a 2  s.com
    return (start.getNodeType() == Node.ELEMENT_NODE) ? (Element) start : null;
}

From source file:Main.java

/** Finds and returns the last child node with the given name. */
public static Element getLastChildElement(Node parent, String elemNames[]) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                if (child.getNodeName().equals(elemNames[i])) {
                    return (Element) child;
                }/* w w  w  . j av a  2s. co  m*/
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * Finds and returns the last child node with the given name and
 * attribute name, value pair.//from   w ww . j a va  2 s  .c o m
 */
public static Element getLastChildElement(Node parent, String elemName, String attrName, String attrValue) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) child;
            if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) {
                return element;
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static final String getComment(Element elem) {
    StringBuffer sb = new StringBuffer();
    Node node = elem.getPreviousSibling();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            break;
        }/*from w w w.j  a v a2s  .c o  m*/
        if (node.getNodeType() == Node.COMMENT_NODE) {
            if (sb.length() > 0) {
                sb.insert(0, '\n');
                sb.insert(0, ((Comment) node).getData());
            } else {
                sb.append(((Comment) node).getData());
            }
        }
        node = node.getPreviousSibling();
    }
    return sb.toString();
}

From source file:Main.java

public static final String getComment(Element elem) {
    StringBuilder sb = new StringBuilder();
    Node node = elem.getPreviousSibling();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            break;
        }/*from  www . java  2  s  . c o  m*/
        if (node.getNodeType() == Node.COMMENT_NODE) {
            if (sb.length() > 0) {
                sb.insert(0, '\n');
                sb.insert(0, ((Comment) node).getData());
            } else {
                sb.append(((Comment) node).getData());
            }
        }
        node = node.getPreviousSibling();
    }
    return sb.toString();
}

From source file:Main.java

/** Finds and returns the last child node with the given qualified name. */
public static Element getLastChildElementNS(Node parent, String[][] elemNames) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                String uri = child.getNamespaceURI();
                if (uri != null && uri.equals(elemNames[i][0])
                        && child.getLocalName().equals(elemNames[i][1])) {
                    return (Element) child;
                }//from  w ww .j  ava  2s .  co m
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:XMLUtils.java

/**
 * Get the previous sibling element of a given element.
 * @param el//  ww w  .j  a v  a2s . c  om
 * @return
 */
public static Element getPrevious(Element el) {
    Node n = el.getPreviousSibling();
    while (n != null && !(n instanceof Element)) {
        // get the next one
        n = n.getPreviousSibling();
    }

    if (n instanceof Element) {
        return (Element) n;
    } else {
        // else, nothing to return
        return null;
    }
}

From source file:XMLUtils.java

/**
 * Get the previous sibling element of a given element.
 * @param el//  www . j av a 2  s.c  o  m
 * @return
 */
public static Element getPreviousSibling(Element el) {
    Node n = el.getPreviousSibling();
    while (n != null && (!(n instanceof Element) || !el.getTagName().equals(((Element) n).getTagName()))) {
        // get the next one
        n = n.getPreviousSibling();
    }

    if (n instanceof Element) {
        return (Element) n;
    } else {
        // else, nothing to return
        return null;
    }
}

From source file:Main.java

/**
 * Gets Node Index value for the XPath expression<br>
 * <p/>//from  ww w . j a  va 2  s  .com
 * e.g. <root><a><b>ritesh</b><b>trivedi</b></a></root> calling
 * <p/>
 * getXPathNodeIndex for Node with value
 * <p/>
 * trivedi would return 2
 */

public static int getXPathNodeIndex(Node node, boolean ignoreWhitespace)

{

    int nodeIndex = 0;

    if (node == null)

    {

        return -1;

        //throw new IllegalArgumentException("Node argument for getXPathNodeIndex cannot be null");

    }

    Node prevNode = node;

    //log("getXPathNodeIndex info next few lines");            

    //log("Current node:");

    //printNode(node);

    while ((prevNode = prevNode.getPreviousSibling()) != null)

    {

        //log("previous node");

        //printNode(prevNode);

        if (nodesEqual(node, prevNode, ignoreWhitespace))

            nodeIndex++;

    }

    // If similar children are found, ONLY then increase

    // the nodeIndex by 1 since XPath exprn starts at 1 and not 0

    if (nodeIndex > 0)

        nodeIndex++;

    if (nodeIndex == 0)

    {

        Node nextNode = node;

        boolean found = false;

        while (((nextNode = nextNode.getNextSibling()) != null) && (!found))

        {

            //log("Next node");

            //printNode(nextNode);

            if (nodesEqual(node, nextNode, ignoreWhitespace))

            {

                nodeIndex++;

                found = true;

            }

            //node = prevNode;

        }

    }

    return nodeIndex;

}