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

/**
 * Finds the first (direct) child Element with a given tag name.
 * /*w  w  w. j  a  v a 2s. c  o  m*/
 * @param parent the parent element below which to search the child
 * @param tagName the (tag) name of the desired child element
 * @return the child element if an element of that name existed, or null otherwise
 */
static public Element findFirstChild(Node parent, String tagName) { // Child Element suchen
    if (parent == null)
        return null;
    Node node = parent.getFirstChild();
    while (node != null) { // Find all Element nodes
        if (node.getNodeType() == Node.ELEMENT_NODE) { // check name
            Element elem = (Element) node;
            if (tagName.equalsIgnoreCase(elem.getTagName()))
                return elem; // found
        }
        node = node.getNextSibling();
    }
    return null; // not found!
}

From source file:Main.java

private static String getSubTagValue(Node node, String subTagName) {
    String returnString = "";
    if (node != null) {
        NodeList children = node.getChildNodes();
        for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
            Node child = children.item(innerLoop);
            if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName)) {
                Node grandChild = child.getFirstChild();
                if (grandChild.getNodeValue() != null)
                    return grandChild.getNodeValue();
            }/*from   w  ww.ja v  a  2 s.  c o m*/
        } // end inner loop
    }
    return returnString;
}

From source file:Main.java

/**
 * Remove <tt>xml:base</tt> attributes from the given node and all of its
 * descendants. According to section 1.3.4 of the DOM Level 3 Core
 * specification, when expanding an external entity reference, DOM parser
 * will add <tt>xml:base</tt> attributes to all elements appearing in the
 * external entity. These attributes don't appear in the reference output
 * files provided in the test suite (see
 * {@link XMLConformanceTest#getOutput()}). This method can be used to
 * remove <tt>xml:base</tt> attributes in DOM documents.
 * //from  w  w  w  .ja  v  a 2 s.c o m
 * @param node
 *            the node to process
 */
public static void removeXmlBaseAttributes(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        element.removeAttribute("xml:base");
    }
    Node child = node.getFirstChild();
    while (child != null) {
        removeXmlBaseAttributes(child);
        child = child.getNextSibling();
    }
}

From source file:Main.java

/***************************************************************************
 * Returns the value of the first child under the give element with the
 * given name.//  w  w w  . j  a va 2 s. co  m
 * 
 * @param e
 * @param name
 * @return
 * @throws Exception
 **************************************************************************/
public static String getChildValueByName(Element e, String name) throws Exception {
    String s = "Not found";

    /*
     * The getElementsByTagName() function returns ANY children under the
     * given element with the given tag name. This function is intended to
     * return the value of only an immediate child with a given name.
     */
    NodeList childNodes = e.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (node.getNodeName().equals(name)) {
            if (node.getFirstChild() != null)
                s = node.getFirstChild().getNodeValue();
            else
                s = "";
            break;
        }
    }

    return s;
}

From source file:eu.planets_project.services.utils.cli.CliMigrationPaths.java

private static String decodeCommandNode(Node command) {
    Node commandtext = command.getFirstChild();
    if (commandtext.getNodeType() == Node.TEXT_NODE) {
        return commandtext.getNodeValue();
    }/* w w  w . j  a  v a  2 s. c  o  m*/
    return "";
}

From source file:Main.java

private static String getSubTagValue(Node node, String subTagName) {
    String returnString = "";
    if (node != null) {
        NodeList children = node.getChildNodes();
        for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
            Node child = children.item(innerLoop);
            if ((child != null) && (child.getNodeName() != null) && (child.getNodeName().equals(subTagName))) {
                Node grandChild = child.getFirstChild();
                if (grandChild.getNodeValue() != null) {
                    return grandChild.getNodeValue();
                }//from  w  w w .j  a v  a 2s  . c om
            }
        }
    }
    return returnString;
}

From source file:Main.java

/***************************************************************************
 * Sets the value of the first child under the given element with the given
 * name. If the child has no nodes, one is created.
 * /* ww w . jav  a2s  .co m*/
 * @param doc
 * @param e
 * @param name
 * @param value
 * @return
 * @throws Exception
 **************************************************************************/
public static boolean setChildValueByName(Document doc, Element e, String name, String value) throws Exception {
    NodeList childNodes = e.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (node.getNodeName().equals(name)) {
            Node child = node.getFirstChild();
            if (child == null)
                ((Element) node).appendChild(doc.createTextNode(value));
            else
                child.setNodeValue(value);

            return true;
        }
    }

    return false;
}

From source file:Main.java

public static String getElementValue(final Node sourceNode) {
    String result = null;/*from   www . j  a v  a  2  s .c om*/
    if (sourceNode == null) {
        return result;
    }

    if (sourceNode.getNodeType() == Node.ELEMENT_NODE) {
        Node firstChild = sourceNode.getFirstChild();

        if (firstChild != null) {
            result = firstChild.getNodeValue();
        }
    }

    return result;
}

From source file:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java

private static Node findFirstChildNamed(Node parent, String localName) {
    Node n = parent.getFirstChild();
    while (n != null && ((n.getNodeType() != Node.ELEMENT_NODE) || !n.getLocalName().equals(localName))) {
        n = n.getNextSibling();/*  w w  w  .  ja v  a 2s.  co  m*/
    }
    return n;
}

From source file:DomUtil.java

/**
 * Get the first direct child with a given type
 *//*  w w w .j  a va  2 s .c o m*/
public static Node getChild(Node parent, int type) {
    Node n = parent.getFirstChild();
    while (n != null && type != n.getNodeType()) {
        n = n.getNextSibling();
    }
    if (n == null)
        return null;
    return n;
}