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

/**
 * Find an immediate child of the given name
 *//*from   w  ww.j av  a 2  s.co  m*/
public static Element findImmediateChildElement(Node node, String name) {

    Element found = null;

    if (node != null) {

        for (Node child = node.getFirstChild(); child != null
                && found == null; child = child.getNextSibling()) {

            if (child.getNodeType() == Node.ELEMENT_NODE) {
                Element tmp = (Element) child;
                if (tmp.getTagName().equals(name)) {
                    return tmp;
                }
            }
        }
    }

    return found;
}

From source file:Utils.java

/**
 * Get the specified text node associated with this element
 * @param parent the node containing text
 * @param number The text node to fetch (1st, 2nd, etc)
 * @return Text (trimmed of leanding/trailing whitespace, null if none)
 *//*from   www . ja  v a  2 s. c om*/
public static String getTextNodeByNumber(Node parent, int number) {
    String text = null;
    int count = 1;

    if (parent != null) {
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {

            if ((child.getNodeType() == Node.TEXT_NODE) && (count++ == number)) {
                text = child.getNodeValue();
                return text.trim();
            }
        }
    }
    return text;
}

From source file:Main.java

public static Element childNodeByTag(Node node, String childNodeName) {
    if (node == null)
        return null;

    Element childElement = null;/*w ww.ja v a2 s  .com*/
    Node childNode = node.getFirstChild();
    if (childNode != null) {
        do {
            if (childNode.getNodeType() == Node.ELEMENT_NODE
                    && (childNodeName == null || childNodeName.equals(childNode.getNodeName()))) {
                return (Element) childNode;
            }
        } while ((childNode = childNode.getNextSibling()) != null);
    }

    return null;
}

From source file:Main.java

public static List childNodeList(Node node) {
    if (node == null)
        return null;

    List children = new LinkedList();
    Node childNode = node.getFirstChild();
    if (childNode != null) {
        do {/*from   w  w  w  . ja  va2  s.c o  m*/
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                children.add(childNode);
            }
        } while ((childNode = childNode.getNextSibling()) != null);
    }

    return children;
}

From source file:Main.java

/**
 * /*from   w ww .  j  a va 2  s.com*/
 * @param element
 * @param tagname
 * @param valueRange
 * @return List
 */
public static List<String> getDOMElementTextByTagName(final Element element, String tagname,
        final int valueRange) {
    final List<String> list = new ArrayList<>();

    if (element != null) {
        // Just to ensure we pick off by the name of the child node...
        final NodeList nodeList = element.getElementsByTagName(tagname.substring(tagname.lastIndexOf("/") + 1)); //$NON-NLS-1$

        if (nodeList.getLength() == 0) {
            list.add(null);
        }

        for (int i = 0; i < ((valueRange == ALL_VALUES) ? nodeList.getLength() : 1); i++) {
            final Node node = nodeList.item(i);

            if (node != null) {
                final Node text = node.getFirstChild();
                list.add(text.getNodeValue());
            }
        }
    } else {
        list.add(null);
    }

    return list;
}

From source file:Main.java

/**
 * Retrieves the text of a given element.
 * /*from  w  ww. j a  v  a 2s.  com*/
 * @param elem the Element for which the text value is requested
 * @return the text value of that element or null if the element has no text value
 */
static public String getElementText(Node elem) {
    String value = null;
    Node node = (elem != null) ? elem.getFirstChild() : null;
    // Find Text
    while (node != null) { // Find all Text nodes
        if (node.getNodeType() == Node.TEXT_NODE) { // set or append
            if (value == null)
                value = node.getNodeValue();
            else
                value += node.getNodeValue();
        }
        node = node.getNextSibling();
    }
    return value;
}

From source file:Main.java

/**
 * Gets the first (direct) child Element.
 * //w  ww . j a  va  2s . c o m
 * @param parent the parent element below which to search the child
 * @return the first child element, or null otherwise
 */
static public Element getFirstChild(Node parent) { // 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)
            return (Element) node; // found
        node = node.getNextSibling();
    }
    return null; // not found!
}

From source file:com.iggroup.oss.restdoclet.plugin.util.XmlUtils.java

/**
 * Returns the test-content of a XML node. Leading and trailing whitespaces
 * are removed in the text./*from   w w  w  . j a v  a  2s.  co m*/
 * 
 * @param node the XML node.
 * @return the text-content or <code>null</code> if the node does not
 *         contain any text.
 */
public static String textContext(final Node node) {
    String result;
    if (node.getFirstChild() == null) {
        result = null;
    } else {
        result = trimToNull(node.getFirstChild().getNodeValue());
    }
    return result;
}

From source file:Main.java

public static String getNodeValue(Node n) {
    if (n == null)
        return null;

    String val = n.getNodeValue();
    if (val == null) {
        Node child = n.getFirstChild();
        if (child != null) {
            val = child.getNodeValue();
        }/*ww w  . j av  a 2 s. c om*/
    }
    return val;
}

From source file:com.spun.util.ups.UPSUtils.java

/***********************************************************************/
private static UPSQuote extractQuote(Node node) {
    String service;/*  w w  w  .j a  v a2 s  . c  o  m*/
    double cost = 0.0;
    Node serviceNode = getNodeByName(node, "Service");
    Node code = getNodeByName(serviceNode, "Code");
    service = code.getFirstChild().getNodeValue();
    Node totalCharges = getNodeByName(node, "TotalCharges");
    Node monetaryValue = getNodeByName(totalCharges, "MonetaryValue");
    cost = NumberUtils.load(monetaryValue.getFirstChild().getNodeValue(), 0.0);
    return new UPSQuote(UPSServiceType.getForCode(service), cost);
}