Example usage for org.w3c.dom Node ELEMENT_NODE

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

Introduction

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

Prototype

short ELEMENT_NODE

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

Click Source Link

Document

The node is an Element.

Usage

From source file:Main.java

public static Element findPreviousElement(Node current, boolean sameName) {
    String name = null;//from  ww w  .  java  2 s.  c o m
    if (sameName) {
        name = current.getNodeName();
    }
    int type = Node.ELEMENT_NODE;
    return (Element) getPrevious(current, name, type);
}

From source file:Main.java

/**
 * @param child/*from   www. ja  v  a2 s . c  o m*/
 * @return the single child element or null
 * @throws Exception if the child is present multiple times
 */
public static Element getChild(Element element, String child) throws Exception {
    NodeList nodes = element.getChildNodes();
    Element ret = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeName().equalsIgnoreCase(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (ret != null) {
                throw new Exception("Child element '" + child + "' present multiple times");
            } else {
                ret = (Element) childNode;
            }
        }
    }
    if (ret == null) {
        return null;
    } else {
        return ret;
    }
}

From source file:Main.java

public static Element getChildElement(Element elt, String name) {
    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            if (name.equals(node.getNodeName()))
                return (Element) node;
    }// ww w  . j ava  2s  . co m
    return null;
}

From source file:Main.java

/**prints the document tree
 * @param node node to start at/*from  w w w.ja  v  a  2 s .c om*/
 * @param ident amount of indention*/
public static void printTree(Node node, int ident) {
    if (node == null)
        return;
    NodeList children;
    System.out.print("Node: " + node.getNodeName() + " ");
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node");
        break;

    case Node.ELEMENT_NODE:
        System.out.println("Element Node");
        break;

    case Node.TEXT_NODE:
        System.out.println("->" + node.getNodeValue().trim() + "<-");
        break;

    case Node.CDATA_SECTION_NODE:
        System.out.println("CData Node");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Proposing Instruction Node");
        break;

    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Node");
        break;

    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Node");
        break;

    default:
    }

    for (int j = 0; j < 2 * ident; j++)
        System.out.print(" ");
    System.out.println("It has the following Children");
    children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            for (int j = 0; j < ident; j++)
                System.out.print(" ");
            System.out.print("Child " + ident + "." + i + " = ");
            printNodeType(children.item(i), ident + 1);
        }
        System.out.println();
    }
}

From source file:Main.java

public static Element findElementNodeByName(Node node, String name) {
    if (node == null) {
        return null;
    }/*from   w  w  w . j  a  v  a  2  s  .  co  m*/
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null || nodeList.getLength() == 0) {
        return null;
    }

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node item = nodeList.item(i);
        if (item != null
                && (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE)
                && name.equalsIgnoreCase(item.getNodeName())) {
            return (Element) item;
        } else {
            Element element = findElementNodeByName(item, name);
            if (element != null) {
                return element;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets the previous comment./*w  w  w.  j a  v a  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:Utils.java

/**
 * <p>Returns the first child element with the given name. Returns
 * <code>null</code> if not found.</p>
 *
 * @param parent parent element// w  w w . ja va2  s . c om
 * @param name name of the child element
 * @return child element
 */
public static Element getChildElementByName(Element parent, String name) {
    NodeList children = parent.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.getTagName().equals(name)) {
                return element;
            }
        }
    }

    return null;
}

From source file:Main.java

static public Element selectSingleElement(Element element, String xpathExpression) throws Exception {
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                return (Element) node;
            }//from  w  w  w  .  j  av  a  2s . c o m
        }
        //  NodeList nodes = element.getElementsByTagName(xpathExpression);
        //  if (nodes.getLength() > 0) {
        //      return (Element) nodes.item(0);
        //  } else {
        return null;
        //  }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element node = (Element) xpath.evaluate(xpathExpression, element, XPathConstants.NODE);
        return node;
    }
}

From source file:Main.java

public static Element[] getChildElementNodes(Node node) {
    if (node == null) {
        return null;
    }/* ww  w.j  a  va 2 s . co  m*/

    ArrayList<Element> elements = new ArrayList<>();
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null && item.getNodeType() == Node.ELEMENT_NODE) {
                elements.add((Element) item);
            }
        }
    }
    return elements.toArray(new Element[elements.size()]);
}

From source file:Main.java

/**
 * Indicates whether element has any child element.
 *
 * @param element the namespace to analyze.
 * @return true if element has any child element otherwise false.
 *///from  w w  w  .j a va 2 s .  c  o  m
public static boolean hasChildElements(Element element) {
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }
    }
    return false;
}