Java XML Node Previous getPreviousComment(Node element)

Here you can find the source of getPreviousComment(Node element)

Description

Gets the previous comment.

License

Open Source License

Parameter

Parameter Description
element the element

Return

the previous comment

Declaration

public static String getPreviousComment(Node element) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Node;

public class Main {
    /**//from   ww w  .  j a  v a2 s  . co m
     * Gets the previous comment.
     * 
     * @param element
     *            the element
     * @return the previous comment
     */
    public static String getPreviousComment(Node element) {
        while (element.getPreviousSibling() != null) {
            Node prev = element.getPreviousSibling();
            if (prev.getNodeType() == Node.COMMENT_NODE) {
                return prev.getTextContent();
            } else if (prev.getNodeType() == Node.TEXT_NODE) {
                return getPreviousComment(prev);
            } else if (prev.getNodeType() == Node.ELEMENT_NODE) {
                return null;
            }
        }
        return null;
    }
}

Related

  1. getAncestorNode(Node visualNode, String tagName)
  2. getAncestors(Node node)
  3. getPrevious(final Node current, final boolean sameName)
  4. getPrevious(Node node)
  5. getPreviousComment(Node element)
  6. getPreviousElementNode(Node node)
  7. getPreviousNodeByName(Node currentNode, String tagName)
  8. getPreviousTypedNode(Node node, short nodeType)
  9. getPreviousTypedNode(Node node, short nodeType)