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

private static void removeEmptyTextNodes(Node parentNode) {
    Node childNode = parentNode.getFirstChild();
    while (childNode != null) {
        // grab the "nextSibling" before the child node is removed
        Node nextChild = childNode.getNextSibling();

        short nodeType = childNode.getNodeType();
        if (nodeType == Node.TEXT_NODE) {
            boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty();
            if (containsOnlyWhitespace) {
                parentNode.removeChild(childNode);
            }/*from  ww w  .ja  v a2 s .c o  m*/
        }
        childNode = nextChild;
    }
}

From source file:Main.java

static String getNodeValue(final Node node) {
    if (node != null && node.getFirstChild() != null && node.getFirstChild().getNodeValue() != null) {
        return node.getFirstChild().getNodeValue().trim();
    }/*from   www  .  j  a  v  a 2 s .com*/
    return null;
}

From source file:Main.java

public static String getTextElementValue(String elementName, Element parentElement, boolean showEmptyAsNull) {
    ArrayList<Node> childNodesWithTagName = getChildNodesWithTagName(elementName, parentElement);

    if (childNodesWithTagName.size() > 0) {
        Node childNode = childNodesWithTagName.get(0);
        String text = childNode.getFirstChild().getNodeValue().trim();

        return text.equals("") && showEmptyAsNull ? null : text;
    }/*w  w w  . j  a  va2  s.c  om*/

    return null;
}

From source file:Main.java

/**
 * Returns all direct simple text value subnodes and their values for a dataNode
 *
 * Example XML: <dataNode> <a>1</a> <b>2</b> <c>3</c> </dataNode> Returns: a=1, b=2, c=3.
 *
 * @param dataNode/*from  w ww.ja v a 2 s  . c  o m*/
 *            the data node
 * @return the simple values of node
 */
public static Map<String, String> getSimpleValuesOfNode(Node dataNode) {
    Map<String, String> returnMap = new HashMap<String, String>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getFirstChild() != null && node.getFirstChild().getNodeType() == Node.TEXT_NODE
                && node.getChildNodes().getLength() == 1) {
            returnMap.put(node.getNodeName(), node.getFirstChild().getNodeValue());
        }
    }

    return returnMap;
}

From source file:Main.java

/**
 * sets the value of an Element in the Xml Document.
 * finds the first occurance of the element and sets its value.
 * @param root the root Element./*w w w  .j a  v  a  2 s .c  o m*/
 * @param elemName the name of the element to search for.
 * @return String the element value or null if not found.
 */
public static void setNodeValue(Document doc, String elemName, String value) {
    Element root = doc.getDocumentElement();
    NodeList nl = root.getElementsByTagName(elemName);
    if (null != nl) {
        Node n = nl.item(0);
        if (null != n) {
            n.getFirstChild().setNodeValue(value);
        } else {
            addNode(doc, elemName, value);
        }
    } else {
        addNode(doc, elemName, value);
    }

}

From source file:Main.java

private static void cleanEmptyTextNodes(Node parentNode) {
    boolean removeEmptyTextNodes = false;
    Node childNode = parentNode.getFirstChild();
    while (childNode != null) {
        removeEmptyTextNodes |= checkNodeTypes(childNode);
        childNode = childNode.getNextSibling();
    }//  w  ww .  jav a2s.  c o m

    if (removeEmptyTextNodes) {
        removeEmptyTextNodes(parentNode);
    }
}

From source file:Main.java

/**
 * Extracts a Long from a document that consists of a Long only.
 * //from   w  w w.j  av  a  2  s.c o  m
 * @param doc
 * @return the Long
 */
public static Long extractLong(Node doc) {
    if (doc == null) {
        return 0l;
    }
    return Long.parseLong(doc.getFirstChild().getTextContent());
}

From source file:Main.java

public static String getTagValue(Element root, String tagName) {
    String returnString = "";
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        if (node != null) {
            Node child = node.getFirstChild();
            if ((child != null) && (child.getNodeValue() != null)) {
                return child.getNodeValue();
            }//from   w w w  .ja  v a  2  s.  c om
        }
    }
    return returnString;
}

From source file:Main.java

public static Set<Node> getChildrenElements(Node node) {
    final Set<Node> result = new LinkedHashSet<>();

    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() != Node.TEXT_NODE) {
            result.add(child);/*from w  ww.  j  a  va2 s.  com*/
        }
    }

    return result;
}

From source file:Main.java

/**
 * A method to get the value of desire node from xml document
 * @param Node parent, xml's node object to get
 * @return String , value from provided node
 *///from www  .j a v a 2  s.com
static public String getNodeValue(Node parent) {
    String ret = "";

    Node n = parent.getFirstChild();
    while (n != null) {
        if (n.getNodeType() == Node.TEXT_NODE) {
            try {
                ret = n.getNodeValue().trim();
            } catch (NullPointerException ex) {
                ret = "";
                break;
            }
        }
        n = n.getNextSibling();
    }
    return (ret);
}