Java XML Node Previous getPreviousComment(Node element)

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

Description

get Previous Comment

License

Open Source License

Declaration

public static String getPreviousComment(Node element) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006-2010 eBay Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/

import org.w3c.dom.Node;

public class Main {
    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;
            }/*from   w w w .  j  ava  2 s.c o m*/
        }
        return null;
    }
}

Related

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