Example usage for org.w3c.dom Node getNodeType

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

Introduction

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

Prototype

public short getNodeType();

Source Link

Document

A code representing the type of the underlying object, as defined above.

Usage

From source file:Main.java

/**
 * Determine whether a single child is available of a particular type.
 * @return true if the specified child element is present
 * @throws Exception if the child is present multiple times.
 *///from   w  w w . ja  v a 2 s. co m
public static boolean hasChild(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().equals(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (ret != null) {
                throw new Exception("Child element '" + child + "' present multiple times");
            } else {
                ret = (Element) childNode;
            }
        }
    }
    return ret != null;
}

From source file:Main.java

/**
 * Get the first child element of the specified (root) element that has the specified name. If no element by the
 * specified name is found, null is returned. Note that more than one element with the same name may be a child of
 * root. This method simply returns the first one.
 * /*from  ww w  .  ja  v a 2 s .com*/
 * @param root The element to search.
 * @param name The local name of the element to look for.
 * @return The element if found, null otherwise.
 */
public static Element getElement(Element root, String name) {
    if (root == null || name == null || name.length() <= 0)
        throw new IllegalArgumentException("Null or invalid argument passed to XmlUtil.getElement()");
    NodeList lst = root.getChildNodes();
    int size = lst.getLength();
    name = localName(name);
    for (int i = 0; i < size; i++) {
        Node node = lst.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = localName(node);
            if (name.equals(nodeName))
                return (Element) node;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Checks if the given node has only text children.
 *
 * @param node parent.//from w  w  w  .  j a v a 2 s. com
 * @return 'TRUE' if the given node has only text children; 'FALSE' otherwise.
 */
public static boolean hasOnlyTextChildNodes(final Node node) {
    boolean result = true;
    final NodeList children = node.getChildNodes();
    for (int i = 0; result && i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (child.getNodeType() != Node.TEXT_NODE) {
            result = false;
        }
    }

    return result;
}

From source file:Main.java

public static String getNodeAttr(String tagName, String attrName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if ((data.getNodeType() == Node.ATTRIBUTE_NODE)
                        && data.getNodeName().equalsIgnoreCase(attrName)) {
                    return data.getNodeValue();
                }/*from   ww  w.  jav a 2s  .  c om*/
            }
        }
    }
    return "";
}

From source file:Main.java

/**
 * Gets the given node's children of the given type.
 *
 * @param node parent.// w ww  . ja  v  a2 s. com
 * @param nodetype searched child type.
 * @return children.
 */
public static List<Node> getChildNodes(final Node node, final short nodetype) {
    final List<Node> result = new ArrayList<Node>();

    final NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (child.getNodeType() == nodetype) {
            result.add(child);
        }
    }

    return result;
}

From source file:Main.java

public static void walkNodes(Node nodeIn, StringBuffer sb, String sPad) {
    if (nodeIn == null)
        return;// w ww.  ja va2s  .c  o m
    NamedNodeMap map = nodeIn.getAttributes();
    if (map != null)
        for (int i = 0; i < map.getLength(); i++) {
            Node n = map.item(i);
            if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
                sb.append(sPad + "   Attribute:" + n.getNodeName() + " = " + n.getNodeValue() + '\n');
            }
        }
    NodeList nodes = nodeIn.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(sPad + "Element: " + n.getNodeName() + '\n');
        }
        if (n.getNodeType() == Node.TEXT_NODE) {
            sb.append(sPad + "   Value: = " + n.getNodeValue() + '\n');
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            walkNodes(n, sb, sPad + "   ");
        }
    }
}

From source file:Main.java

static public String getNodeValue(Node node) {
    if (node == null)
        return null;

    if (node.getNodeType() == Node.ELEMENT_NODE)
        return getElementText((Element) node);
    else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
        return getFragmentText((DocumentFragment) node);
    else/*from   w  w  w  . jav  a 2s .  c  o m*/
        return node.getNodeValue();
}

From source file:Main.java

/**
 * Checks if the given Node is Comment Node
 *//* w w  w .j  av  a 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

/**
 * Check whether the given nodes containes an attribute with the given name and value.<br>
 * The name and value of the attribute MUST be the same of the node ones in order to have true as
 * a result.<br> The check is case insensitive.<br>
 * /*from   ww  w .  jav a 2 s.c om*/
 * @param node node to be analyzed
 * @param attrName the attribute name to be matched
 * @param attrValue the attribute value to be matched
 * @return <code>true</code> if node containes the given attributes, <code>false</code> otherwise (even if node is null)
 */
public static boolean nodeAttributeEquals(Node node, String attrName, String attrValue) {

    if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {

        String value = ((Element) node).getAttribute(attrName);
        return (value != null && value.equalsIgnoreCase(attrValue));

    } else {

        return false;

    }
}

From source file:Main.java

public static Element getNode(Element e, String name) {
    Node n = e.getElementsByTagName(name).item(0);
    if (n == null)
        return e;
    else if (n.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) n;
    } else//  ww  w .  j  av a2  s.com
        return e;
}