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:Main.java

public static Element getFirstChildElement(final Node parent) {
    if (parent != null) {
        Node tmpResult = parent.getFirstChild();

        if (tmpResult != null) {
            if (Node.ELEMENT_NODE == tmpResult.getNodeType()) {
                return (Element) tmpResult;
            } else {
                return getNextSiblingElement(tmpResult);
            }// w w  w .jav  a  2 s  .c o m
        }
    }

    return null;
}

From source file:Main.java

/**
 * Gets a node value./*from ww w . j a v  a 2  s.  co m*/
 *
 * @param pNode
 *            the p node
 * @return the node value
 */
public static String getNodeValue(Node pNode) {
    if (pNode.getNodeValue() != null) {
        return pNode.getNodeValue();
    } else if (pNode.getFirstChild() != null) {
        return getNodeValue(pNode.getFirstChild());
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Extracts a {@code Map} of header tag to value from the header node.
 *
 * @param headerNode the header node// w  w w . j  a v a  2 s . c  o  m
 * @return a {@code Map} of header tag to value from the header node.
 */
public static Map<String, String> extractHeaderMap(Node headerNode) {
    Map<String, String> headerMap = new HashMap<String, String>();
    for (int i = 0; i < headerNode.getChildNodes().getLength(); i++) {
        Node headerChildNode = headerNode.getChildNodes().item(i);
        headerMap.put(headerChildNode.getLocalName(), headerChildNode.getFirstChild().getNodeValue());
    }
    return headerMap;
}

From source file:Main.java

/**
 * Method to remove all children of g:options - element
 * /*from   w  w  w  .j av a  2  s  .com*/
 * @param doc document with children  g:options
 * @return document without children g:options
 */
public static Document removeAllOptions(Document doc) {
    NodeList nodes = doc.getElementsByTagName("g:options");
    Node node = nodes.item(0);
    while (node.hasChildNodes())
        node.removeChild(node.getFirstChild());
    return doc;
}

From source file:Main.java

/**
 * Converts a node to a double/* w  w w.jav  a 2  s  . c o  m*/
 * 
 * @param node
 *            The node to be parsed
 * @return The obtained value
 */
protected static Double nodeToDouble(Node node) {
    Double returnValue = null;
    try {
        String nodeValue = node.getFirstChild().getNodeValue();
        nodeValue = nodeValue.trim();
        returnValue = nf.parse(nodeValue).doubleValue();
    } catch (ParseException e) {
        // Should never happen
        e.printStackTrace();
    }

    return returnValue;
}

From source file:Main.java

/**
 * Get the next node in a depth first preorder traversal.
 *
 * <ul>/*from  www  .  j av a2  s.c om*/
 * <li>If the node has a child, return the child
 * <li>Else if the node has a following sibling, return that sibling
 * <li>Else if the node has a following uncle, return that uncle
 * </ul>
 *
 * @param node the current node
 * @return the next node in the preorder traversal
 */
public static Node getNext(Node node) {
    if (node == null)
        return null;

    if (node.getFirstChild() != null)
        return node.getFirstChild();

    for (; node != null; node = node.getParentNode()) {
        if (node.getNextSibling() != null)
            return node.getNextSibling();
    }

    return null;
}

From source file:Main.java

public static Node findSubElement(Node parent, String localName) {
    if (parent == null) {
        return null;
    }// w w w  . j  a v  a  2  s.  com
    Node child = parent.getFirstChild();
    while (child != null) {
        if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getLocalName().equals(localName))) {
            return child;
        }
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * Get the value of specified node./*  w  w  w . j  ava2  s.c  o m*/
 * 
 * @param node
 *          the node
 * @return the value of the node
 */
public static String getNodeValue(Node node) {
    if (null == node) {
        return null;
    }
    if (null == node.getFirstChild()) {
        return null;
    }
    return node.getFirstChild().getNodeValue();
}

From source file:Main.java

public static void setByPath(Node doc, String path, String value) {
    Node node = getNodeByPath(doc, path);
    if (node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE) {
        node.getFirstChild().setTextContent(value);
    } else {/* w w w .  ja va  2s  .c o  m*/
        node.setNodeValue(value);
    }
}

From source file:Main.java

/** Finds and returns the first child node with the given qualified name. */
public static Element getFirstChildElementNS(Node parent, String[][] elemNames) {

    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                String uri = child.getNamespaceURI();
                if (uri != null && uri.equals(elemNames[i][0])
                        && child.getLocalName().equals(elemNames[i][1])) {
                    return (Element) child;
                }/*w w w  .j ava2s .c  o  m*/
            }
        }
        child = child.getNextSibling();
    }

    // not found
    return null;

}