Example usage for org.w3c.dom Node COMMENT_NODE

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

Introduction

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

Prototype

short COMMENT_NODE

To view the source code for org.w3c.dom Node COMMENT_NODE.

Click Source Link

Document

The node is a Comment.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*w  w w . jav  a  2  s .  c o m*/
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    removeAll(doc, Node.ELEMENT_NODE, "junk");

    removeAll(doc, Node.COMMENT_NODE, null);

    doc.normalize();

}

From source file:Main.java

/**
 * Gets the next comment./* w ww  .  j a  va 2s  . c om*/
 * 
 * @param element
 *            the element
 * @return the next comment
 */
public static String getNextComment(Node element) {
    while (element.getNextSibling() != null) {
        Node prev = element.getNextSibling();
        if (prev.getNodeType() == Node.COMMENT_NODE) {
            return prev.getTextContent();
        } else if (prev.getNodeType() == Node.TEXT_NODE) {
            return getNextComment(prev);
        } else if (prev.getNodeType() == Node.ELEMENT_NODE) {
            return null;
        }
    }
    return null;
}

From source file:Main.java

/**
 *///from   w  w w  .  java 2 s  .  c  o  m
public static String getComment(Element searchIn, int commentIndex) {
    NodeList list = searchIn.getChildNodes();
    int counter = 0;
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        if (n.getNodeType() == Node.COMMENT_NODE) {
            if (counter == commentIndex) {
                return n.getTextContent();
            }
            counter++;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets the previous comment./*from  www.ja va  2  s. c  o  m*/
 * 
 * @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;
}

From source file:Main.java

/**
 * @param node//www.  ja v  a2s .c  om
 */
public static void displayNodeInfo(Node node) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node ");
        break;
    case Node.ELEMENT_NODE:
        System.out.println("Element Node: " + node.getNodeName());
        break;
    case Node.TEXT_NODE:
        System.out.println("Text Node: " + node.getNodeName());
        break;
    case Node.CDATA_SECTION_NODE:
        System.out.println("CDATA Section Node: ");
        break;
    case Node.COMMENT_NODE:
        System.out.println("Comment Node ");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Processing Instruction Node ");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Reference Node ");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Type Node ");
        break;
    }
}

From source file:Main.java

/**
 * Checks if the given Node is Comment Node
 *///  w w w .j  a va 2 s  . c  o  m

public static boolean isCommentNode(Node node)

{

    if (node == null)

        return false;

    return (node.getNodeType() == Node.COMMENT_NODE);

}

From source file:Main.java

@SuppressWarnings("null")
public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;//  w  ww.j  a v a 2 s  .c o  m
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                /*
                 if (domimpl && !attr.getSpecified()) {
                 ((Attr) element.getAttributeNode(attrName)).setSpecified(false);
                 }
                 */
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null && dest != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

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 va  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

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 .  ja va 2s.  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

/**
 * Prints a textual representation of the given node to the specified PrintStream.
 *
 * @param  n    Node that is to be printed.
 * @param  out  The PrintStream to which the node is to be printed.
 * @pre    n != null && out != null
 *///  w ww  .j a v  a 2  s  .  c  om
public static void printXMLNode(Node n, PrintStream out) {
    switch (n.getNodeType()) {
    case Node.DOCUMENT_NODE:
        out.println("DOC_ROOT");
        break;

    case Node.ELEMENT_NODE:
        out.println("<" + ((Element) n).getTagName() + ">");
        break;

    case Node.ATTRIBUTE_NODE:
        out.println("@" + ((Attr) n).getName());
        break;

    case Node.TEXT_NODE:
        out.println("\"" + ((Text) n).getWholeText().trim() + "\"");
        break;

    case Node.COMMENT_NODE:
        out.println("COMMENT: \"" + n.getTextContent().trim() + "\"");
        break;

    default:
        out.println("Unknown node type: " + n.getNodeType());
    }
}