Example usage for org.w3c.dom Node getNodeName

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

Introduction

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

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

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   w  ww. j a v a  2s  . c  o m*/
            }
        }
    }
    return "";
}

From source file:Main.java

public static Node findSibling(Node node, String tagName) {
    Node n = node;
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE && !n.getNodeName().equals(tagName))
        n = n.getNextSibling();/*  ww w.  j  a va  2  s.  c om*/
    return n;
}

From source file:Main.java

public static List<Element> extractElementsFromNodeList(NodeList config, String tag, boolean required) {
    List<Element> res = new ArrayList<Element>();
    for (int i = 0; i < config.getLength(); i++) {
        Node n = config.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tag)) {
            res.add((Element) config.item(i));
        }/*from  w  w  w.j  av a2  s  . c  o m*/
    }
    if (required && res.size() == 0) {
        throw new RuntimeException("Tag " + tag + " is required in configuration file");
    }
    return res;
}

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);
        }/*www.  j a  v  a  2 s . c o m*/
    }
    return elements;
}

From source file:Main.java

public static Double[] getDataList(Node sampleNode) {
    if (sampleNode == null)
        return new Double[0];
    List<Double> measureValues = new ArrayList<Double>();
    Node n = sampleNode.getFirstChild();
    while (n != null) {
        if (n.getNodeName().equalsIgnoreCase("data")) {
            String ms = n.getFirstChild().getNodeValue();
            double mesVal = Double.parseDouble(ms);
            measureValues.add(new Double(mesVal));
        }//from   w w  w  . j a v a 2 s. co  m
        n = n.getNextSibling();
    }
    return measureValues.toArray(new Double[0]);
}

From source file:Main.java

/**
 * Finds attribute of node by name.//from   w  ww  .  j  a v  a 2  s  . c o  m
 * @param node Node The context node.
 * @param name String
 * @return Node
 */
public static Node findAttribute(Node node, String name) {
    Node found = null;
    NamedNodeMap nm = node.getAttributes();
    if (nm != null) {
        for (int i = 0; i < nm.getLength(); i++) {
            Node attr = nm.item(i);
            if (attr.getNodeName().compareToIgnoreCase(name) == 0) {
                found = attr;
                break;
            }
        }
    }
    return found;
}

From source file:Main.java

public static List<Node> getChildElementsByTagName(Element ele, String childEleName) {
    NodeList nl = ele.getChildNodes();
    List<Node> childEles = new ArrayList<Node>();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element && childEleName.equals(node.getNodeName())
                || childEleName.equals(node.getLocalName())) {
            childEles.add(node);//from w  w  w  .j  ava  2 s  .c o m
        }
    }
    return childEles;
}

From source file:Main.java

public static List<Element> selectElementsByName(Element parent, String elementName) {
    ArrayList<Element> result = new ArrayList<Element>();

    NodeList list = parent.getChildNodes();

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

        if ((node instanceof Element) && node.getNodeName().equals(elementName)) {
            result.add((Element) node);
        }//from ww  w  . j  av  a 2 s.c o m
    }

    return result;
}

From source file:Main.java

/**
 *  Returns the first child element found with the specified tag name
 *  @param node The node to search//from  w  w w .  j a  va 2 s.c  o  m
 *  @param element The element tag name to search for
 *  @return The matching element Node
 */
public synchronized static Node getFirstElementWithTagName(Node node, String element) {
    if (node != null && element != null) {
        NodeList nl = node.getChildNodes();

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

            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(element))
                return n;
        }
    }

    return null;
}

From source file:Main.java

/**
   Return a list of all the children of a given node with a
   specific name//from   w ww . j av  a  2 s  . com
 */
static public Vector<Node> findAllChildren(Node node, String child_name) {
    if (node == null)
        return null;

    Vector<Node> found_children = new Vector<Node>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name))
            found_children.add(child);
    }
    return found_children;
}