Example usage for org.w3c.dom Element getPreviousSibling

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

Introduction

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

Prototype

public Node getPreviousSibling();

Source Link

Document

The node immediately preceding this node.

Usage

From source file:Main.java

public static Element getPrevSibling(Element e) {
    Node n = e.getPreviousSibling();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getPreviousSibling();//www .j a v  a 2 s .com
    return (Element) n;
}

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 ww  w .j  a v  a 2s  .  com*/
        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 w w  w. j a  v  a  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

/**
 * Get the previous sibling element of the specified element, null if it has no previous other siblings.
 * /*from   ww w  .  j av  a2  s . c  om*/
 * @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

public static Element getPreviousSiblingElement(Element elem) {
    if (elem == null) {
        return null;
    }//w w w. j  a  v a 2  s  .com

    for (Node n = elem.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) n;
        }
    }

    return null;
}

From source file:Main.java

@SuppressWarnings({ "ChainOfInstanceofChecks" })
private static void formatElementStart(/*@Nonnull*/Document document, /*@Nonnull*/Element element, /*@Nonnull*/
        String formatString) {//from w w  w . j  a  v  a  2  s .  c om
    Node previousSibling = element.getPreviousSibling();

    if (previousSibling == null || previousSibling instanceof Element) {
        element.getParentNode().insertBefore(document.createTextNode(formatString), element);
    } else if (previousSibling instanceof Text) {
        Text textNode = (Text) previousSibling;
        String text = textNode.getWholeText();

        if (!formatString.equals(text)) {
            textNode.replaceWholeText(trimRight(text) + formatString);
        }
    }
}

From source file:XMLUtils.java

/**
 * Get the previous sibling element of a given element.
 * @param el/* w ww  . j  a  va  2 s  .c  o  m*/
 * @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//from   w w  w .ja v  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:com.enonic.esl.xml.XMLTool.java

/**
 * Removes a child element from a parent element, and returns the next element to the removed one.
 *
 * @param parent The parent element to the child to be removed.
 * @param child  The child element to remove.
 * @return The next element to the removed child.
 */// ww  w  .  j a va  2s .c  om
public static Element removeChildFromParent(Element parent, Element child) {

    Element previousChild = (Element) child.getPreviousSibling();

    // If the child to remove is the first child
    if (previousChild == null) {
        parent.removeChild(child);
        // Return the first child as the next child
        return (Element) parent.getFirstChild();
    }
    // If the child is not the first child
    else {
        parent.removeChild(child);
        return (Element) previousChild.getNextSibling();
    }
}

From source file:com.wfreitas.camelsoap.SoapClient.java

private Comment getCommentBefore(Element element) {
    Node sibling = element.getPreviousSibling();

    while (sibling != null) {
        if (sibling.getNodeType() == Node.COMMENT_NODE) {
            return (Comment) sibling;
        } else if (sibling.getNodeType() == Node.TEXT_NODE) {
            // continue...
            sibling = sibling.getPreviousSibling();
        } else {//from w  w w . ja  va2  s  . c  om
            // It's an Element, CData, PI etc
            return null;
        }
    }

    return null;
}