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

/** Find the first direct child with a given attribute.
 * @param parent//from   w w w  .  j  a  va 2s  . com
 * @param elemName name of the element, or null for any 
 * @param attName attribute we're looking for
 * @param attVal attribute value or null if we just want any
 */
public static Node findChildWithAtt(Node parent, String elemName, String attName, String attVal) {

    Node child = DomUtil.getChild(parent, Node.ELEMENT_NODE);
    if (attVal == null) {
        while (child != null && (elemName == null || elemName.equals(child.getNodeName()))
                && DomUtil.getAttribute(child, attName) != null) {
            child = getNext(child, elemName, Node.ELEMENT_NODE);
        }
    } else {
        while (child != null && (elemName == null || elemName.equals(child.getNodeName()))
                && !attVal.equals(DomUtil.getAttribute(child, attName))) {
            child = getNext(child, elemName, Node.ELEMENT_NODE);
        }
    }
    return child;
}

From source file:Main.java

public static String path(Node n) {
    if (n == null)
        return "";
    switch (n.getNodeType()) {
    case Node.ATTRIBUTE_NODE: {
        return path(n.getParentNode()) + "/@" + n.getNodeName();
    }/*from w  w  w. j a va  2 s. c  o m*/
    case Node.TEXT_NODE: {
        return path(n.getParentNode()) + "/text()";
    }
    case Node.CDATA_SECTION_NODE: {
        return path(n.getParentNode()) + "/cdata()";
    }
    case Node.ELEMENT_NODE: {
        return path(n.getParentNode()) + "/" + n.getNodeName();
    }
    case Node.DOCUMENT_NODE: {
        return "";
    }
    default: {
        return "";
    }
    }
}

From source file:Main.java

/**
 * Gets the first child element.//from  w w w  .j a va  2s. c  om
 * 
 * @param e the element.
 * @return the first child element.
 */
public static Element getFirstChildElement(Element e) {
    if (e != null) {
        NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) node;
            }
        }
    }
    return null;
}

From source file:Main.java

/** Returns all the direct child elements of the given element. */
public static List<Element> getElements(Element element) {
    List<Element> elements = new ArrayList<>();
    for (Node node : toIterable(element.getChildNodes())) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            elements.add((Element) node);
        }/*from w  w w .ja v a 2  s .co m*/
    }
    return elements;
}

From source file:Main.java

public static final List<Element> getChildElements(Element element) {
    List<Element> childElements = new ArrayList<Element>();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (item.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) item);
        }//from  w  ww .j  a  v a2  s  .  c  o m
    }
    return childElements;
}

From source file:Main.java

public static final Node copyElement(Node e) throws UnsupportedDataTypeException {
    Node result = null;/*  www  .jav a2s  .c o m*/

    switch (e.getNodeType()) {
    case Node.ELEMENT_NODE:
        result = document.createElement(e.getNodeName());

        for (int i = 0; i < e.getAttributes().getLength(); i++) {
            Attr attr = document.createAttribute(e.getAttributes().item(i).getNodeName());
            attr.setNodeValue(e.getAttributes().item(i).getNodeValue());
            result.getAttributes().setNamedItem(attr);
        }
        break;

    case Node.CDATA_SECTION_NODE:
        result = document.createCDATASection(((CDATASection) e).getData());
        break;
    case Node.TEXT_NODE:
        if (((Text) e).getTextContent().replaceAll("\t", "").trim() != "")
            result = document.createTextNode(((Text) e).getTextContent().replaceAll("\t", "").trim());
        break;

    default:
        throw new UnsupportedDataTypeException(new StringBuilder(e.getNodeType()).toString());
    }

    for (int i = 0; i < e.getChildNodes().getLength(); i++)
        result.appendChild(copyElement(e.getChildNodes().item(i)));

    return result;
}

From source file:Main.java

/**
 * Checks if the given {@link NodeList} contains a child element.
 *
 * @param list The {@link NodeList} to check.
 * @return Whether the {@link NodeList} contains children.
 *//*from w w  w . ja va2  s. c  o m*/
public static boolean hasChildElement(NodeList list) {

    Node n;

    for (int i = 0; i < list.getLength(); i++) {
        n = list.item(i);

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }
    }

    return false;

}

From source file:Main.java

@SuppressWarnings("unchecked")
public final static Vector subElementList(Element superEle, String subName) {
    Vector v = new Vector();
    NodeList list = superEle.getChildNodes();
    if (list == null) {
        return null;
    }// w  w w . ja v  a  2 s. c o m

    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(subName)) {
                v.add(node);
            }
        }
    }
    return v;
}

From source file:Main.java

public static List<Element> getChildElements(Element elem, String name) {
    NodeList nodes = elem.getElementsByTagName(name);
    List<Element> elements = new ArrayList<Element>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            elements.add((Element) node);
        }//from www  .jav  a 2s  . c o  m
    }
    return elements;
}

From source file:Main.java

public static List<String> getPropertiesFromXML(Node propNode) {
    ArrayList<String> properties;
    properties = new ArrayList<String>();
    NodeList childList = propNode.getChildNodes();

    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            properties.add(namespace + ":" + nodeName);
        }//  ww  w  . j  av  a  2s. c  o  m
    }
    return properties;
}