Example usage for org.w3c.dom Node getFirstChild

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

Introduction

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

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:Signing.java

private static Element getFirstChildElement(Node node) {
        Node child = node.getFirstChild();
        while ((child != null) && (child.getNodeType() != Node.ELEMENT_NODE)) {
            child = child.getNextSibling();
        }/*  w  w  w.  ja  va 2  s.c om*/
        return (Element) child;
    }

From source file:Main.java

/** Locate a sub-element tagged 'name', return its value.
 *
 *  Will only go one level down, not search the whole tree.
 *
 *  @param element Element where to start looking. May be null.
 *  @param name Name of sub-element to locate.
 *  @param default_value Default value if not found
 *
 *  @return Returns string that was found or default_value.
 *//*  www  .jav a2s .  c om*/
final public static String getSubelementString(final Element element, final String name,
        final String default_value) {
    if (element == null)
        return default_value;
    Node n = element.getFirstChild();
    n = findFirstElementNode(n, name);
    if (n != null) {
        Node text_node = n.getFirstChild();
        if (text_node == null)
            return default_value;
        return text_node.getNodeValue();
    }
    return default_value;
}

From source file:Main.java

public static String getChildNodeValueOf(Node node, String tagName) {
    if (tagName.equals(node.getNodeName())) {
        return getValueOf(node);
    }//from   www. j  a  v  a  2  s  .c om
    for (Node temp = node.getFirstChild(); temp != null; temp = temp.getNextSibling()) {
        if (temp.getNodeType() == Node.ELEMENT_NODE && tagName.equals(temp.getNodeName())) {
            return getValueOf(temp);
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns all child elements of the specified node with the specified name.
 * @param element/*w w  w. j av a2 s. c  o  m*/
 * @param elementName
 * @return the list of child elements
 */
public static List<Element> getChildElements(Node element, String elementName) {
    List<Element> childElems = new ArrayList<Element>();

    for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node instanceof Element) {
            Element childElem = (Element) node;
            String elemName = getElementName(childElem);

            if (elementName.equals(elemName))
                childElems.add(childElem);
        }
    }

    return childElems;
}

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   ww  w.  java  2 s. c om*/
        n = n.getNextSibling();
    }
    return measureValues.toArray(new Double[0]);
}

From source file:Main.java

/**
 * @param node/*from  ww w  .j  av  a  2 s .c  om*/
 * @param name
 * @return a map with all children nodes with the given name
 * mapped by the value of their first child
 */
public static Map<String, Node> findChildrenMapByFirstChild(Node node, String name) {
    Map<String, Node> ret = new HashMap<String, Node>();
    NodeList nl = node.getChildNodes();
    int len = nl.getLength();
    Node child;
    for (int i = 0; i < len; i++) {
        child = nl.item(i);
        if (name.equals(child.getNodeName())) {
            ret.put(child.getFirstChild().getNodeValue(), child);
        }
    }
    return ret;
}

From source file:Main.java

/**
 * Updates the specified the child node//from   ww  w .j a  va2 s. c  o  m
 *  
 * @param parent   the parent node
 * @param nodeName   the name of child node to update
 * @param nodeValue   the new value
 * @return boolean  true 
 * @throws Exception
 */
public static boolean updateChildNodeValueByName(Document doc, Node parent, String nodeName, String nodeValue)
        throws Exception {
    Node node = findChildNodeByName(parent, nodeName);
    if (node == null)
        return false;

    Node firstNode = node.getFirstChild();
    if (firstNode == null) {
        node.appendChild(doc.createTextNode(nodeValue));
    } else {
        firstNode.setNodeValue(nodeValue);
    }
    return true;

}

From source file:Main.java

public static void setByPath(Document doc, String path, Node in) {
    Node node = getNodeByPath(doc, path);
    if (in.getNodeType() == Node.DOCUMENT_NODE) {
        in = in.getFirstChild();
    }/*from  w  w  w  .  j av a 2s.c  om*/
    Node newNode = doc.importNode(in, true);
    node.getParentNode().replaceChild(newNode, node);
}

From source file:Main.java

/** Finds and returns the first child element node. */
public static Element getFirstChildElement(Node parent) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }/*from   ww w  .j  a va  2  s.  c o m*/
        child = child.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * This method is used for updating the value of a tag in a
 * <code>Document</code> object.
 * /*  ww  w .  ja  v a2s  .co m*/
 * @param doc
 *            Document object
 * @param tagName
 *            name of the tag
 * @param tagValue
 *            the updated value of the tag
 */
public static void replaceTagValue(Document doc, String tagName, String tagValue) {
    NodeList nodeList = doc.getElementsByTagName(tagName);
    int j = nodeList.getLength();
    Node node;
    for (int i = 0; i < j; i++) {
        Node newNode = doc.createTextNode(tagValue);
        node = nodeList.item(i);
        if (node.getFirstChild() != null) {
            node.replaceChild(newNode, node.getFirstChild());
        } else {
            node.appendChild(newNode);
        }
    }
}