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

/**
 * Searches the node for content of type Long. If non-long content is found,
 * it logs a warning and returns null./*www. jav  a 2 s .  c  om*/
 */
public static String extractStringFromElement(Node element) {
    if (element == null) {
        return null;
    } else if (element.getFirstChild() == null) {
        return null;
    } else if (element.getFirstChild().getNodeValue() == null) {
        return null;
    } else {
        // Get all the children
        NodeList children = element.getChildNodes();
        StringBuffer output = new StringBuffer();
        for (int n = 0; n < children.getLength(); n++) {
            Node child = children.item(n);
            if (child.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
                output.append(child.getFirstChild().getNodeValue());
            } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
                output.append(child.getFirstChild().getNodeValue());
            } else if (child.getNodeType() == Node.ENTITY_NODE) {
                output.append(child.getFirstChild().getNodeValue());
            } else if (child.getNodeType() == Node.TEXT_NODE) {
                output.append(child.getNodeValue());
            }
        }
        return output.toString().trim();
    }
}

From source file:NodeUtils.java

/**
 * Returns a first child DOM Node of type ELEMENT_NODE
 * for the specified Node.//from  w w  w .  j a  va2 s  .  c  o m
 */
public static Node getChildElementNode(Node xmlNode) {
    if (xmlNode == null || !xmlNode.hasChildNodes()) {
        return null;
    }

    xmlNode = xmlNode.getFirstChild();
    while (xmlNode != null && xmlNode.getNodeType() != Node.ELEMENT_NODE) {
        xmlNode = xmlNode.getNextSibling();
    }

    return xmlNode;
}

From source file:Main.java

private static String getValueByTagName(Node n, String tag) {
    if (n == null)
        return null;
    if (tag.equals(n.getLocalName()))
        return n.getFirstChild().getNodeValue();
    if (n.hasChildNodes())
        return getValueByTagName(n.getFirstChild(), tag);
    else if (n.getNextSibling() != null)
        return getValueByTagName(n.getNextSibling(), tag);
    else// w  w w.j a  v a 2 s.  c  o  m
        return getValueByTagName(n.getParentNode().getNextSibling(), tag);
}

From source file:Main.java

public static String text(Node node) {
    StringBuffer sb = new StringBuffer();
    Node n = node.getFirstChild();
    while (n != null) {
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE)
            sb.append(n.getNodeValue());
        n = n.getNextSibling();/*from  www.  j av  a  2  s.c  o  m*/
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Get the Value of a node that has an embedded text node as a child
 * @param doc the Document to search for the node name
 * @param nodeName the name of the node to get the value
 * @return the value of the text node child
 *//* w  w  w .  jav a  2 s  .  com*/
public static String getNodeTextValue(Document doc, String nodeName) {
    String value = null;
    NodeList nodes = doc.getElementsByTagName(nodeName);
    if (nodes.getLength() > 0) {
        Node node = nodes.item(0);
        if (node.hasChildNodes()) {
            Node text = node.getFirstChild();
            value = text.getNodeValue();
        }
    }
    return replaceSpecialCharacters(value);
}

From source file:Main.java

/** Goes through and adds newlines and indent to the current node and all its children
 * @param current the current node/*  w  w w.ja  v a2s  . c  o m*/
 * @param indent the current level of indent this is increased recursively*/
private static void addFormatting(Document doc, Node current, String indent) {

    // go through each of the children adding space as required
    Node child = current.getFirstChild();
    String childIndent = indent + "\t";
    while (child != null) {
        Node nextChild = child.getNextSibling();
        if (child.getNodeType() != Node.TEXT_NODE) {
            // Only if we aren't a text node do we add the space
            current.insertBefore(doc.createTextNode("\n" + childIndent), child);
            if (child.hasChildNodes()) {
                addFormatting(doc, child, childIndent);
            }
            if (nextChild == null) {
                // Because this is the last child, we need to add some space after it
                current.appendChild(doc.createTextNode("\n" + indent));
            }
        }
        child = nextChild;
    }
}

From source file:Main.java

public static boolean hasChildNS(Node parent, String ns, String name) {
    if (parent == null)
        return false;
    for (Node c = parent.getFirstChild(); c != null; c = c.getNextSibling()) {
        if (isA(c, ns, name))
            return true;
    }//ww  w .  ja va2s. c o m
    return false;
}

From source file:Main.java

/**
 * Get the value of a node assuming there is one child node with a text value
 * //from   w ww.ja v  a2s .  c o  m
 * @param node
 * @return the value
 */
public static String getNodeTextValue(Node node) {
    String value = null;
    if (node.hasChildNodes()) {
        Node text = node.getFirstChild();
        value = text.getNodeValue();
    }
    return replaceSpecialCharacters(value);
}

From source file:Main.java

public static boolean hasDescendantNS(Node parent, String ns, String name) {
    if (parent == null)
        return false;
    for (Node c = parent.getFirstChild(); c != null; c = c.getNextSibling()) {
        if (isA(c, ns, name))
            return true;
        if (hasDescendantNS(c, ns, name))
            return true;
    }//from  w ww.j  av a2 s  .  c  om
    return false;
}

From source file:Main.java

/**
 * @param sibling/*from  ww w .ja  v  a2 s .  co m*/
 * @param uri
 * @param nodeName
 * @param number
 * @return nodes with the constrain
 */
public static Text selectNodeText(Node sibling, String uri, String nodeName, int number) {
    Node n = selectNode(sibling, uri, nodeName, number);
    if (n == null) {
        return null;
    }
    n = n.getFirstChild();
    while (n != null && n.getNodeType() != Node.TEXT_NODE) {
        n = n.getNextSibling();
    }
    return (Text) n;
}