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

/**
 * @param colorNode//from   w  w  w .j a  v  a  2  s .  co  m
 * @return the alpha value
 */
public static String toAlphaValue(Node colorNode) {
    String value = "1";

    if (colorNode == null || colorNode.getFirstChild() == null) {
        return value;
    }

    try {// FIXME no good to grab 1st child than node val
        String nodeVal = colorNode.getFirstChild().getNodeValue();

        value = String.valueOf(Double.parseDouble(nodeVal) / 255d);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // don't care about number formating, just trim the string
    if (value.length() > 6) {
        value = value.substring(0, 5);
    }
    return value;
}

From source file:Main.java

static StringBuffer textContent(Node node, StringBuffer s) {
    if (node == null)
        return s;
    for (Node c = node.getFirstChild(); c != null; c = c.getNextSibling()) {
        if (c.getNodeType() == Node.CDATA_SECTION_NODE) {
            s.append(((CDATASection) c).getNodeValue());
        } else if (c.getNodeType() == Node.TEXT_NODE) {
            s.append(((Text) c).getNodeValue());
        } else {//from   w ww.j a  v  a  2 s .  c o  m
            textContent(c, s);
        }
    }
    return s;
}

From source file:Main.java

public static List<Element> getChildElemsListByName(final String name, final Node parent) {
    ArrayList<Element> v = new ArrayList<Element>();
    Element elem = null;//from  w ww. j  av  a 2 s .co m
    for (Node childNode = parent.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (childNode.getNodeName() == name) {
                elem = (Element) childNode;
                v.add(elem);
            }
        }
    }
    return v;
}

From source file:Main.java

/**
 * Returns the First child element or null if none found
 *
 * @param node//from   ww w .  j a  va  2s.c  o  m
 *            The node. May be null.
 * @return the First child element or null if none found
 */
public static Element getFirstChildElement(Node node) {
    if (node == null) {
        return null;
    }
    Node child = node.getFirstChild();
    if (child instanceof Element) {
        return (Element) child;
    } else {
        return getNextElement(child);
    }
}

From source file:Utils.java

public static String getTextNodeByNumber(Node parent, int number) {
    String text = null;/* w  w  w .j a v a 2  s .com*/
    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

/**
 * Finds and returns the first child node with the given name and
 * attribute name, value pair.//from ww  w . j  a  v a2 s .  c  o m
 */
public static Element getFirstChildElement(Node parent, String elemName, String attrName, String attrValue) {

    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) child;
            if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) {
                return element;
            }
        }
        child = child.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * @param element//from w  ww.  ja  v a2  s . co  m
 * @return all child elements of the specified node.
 */
public static List<Element> getChildElements(Node element) {
    List<Element> childElems = new ArrayList<Element>();

    for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node instanceof Element) {
            childElems.add((Element) node);
        }
    }

    return childElems;
}

From source file:Main.java

static public Element findFirstChildElement(Node parent, String name) {
    if (parent == null)
        return null;
    return findChildElement(parent.getFirstChild(), null, name);
}

From source file:Main.java

public static Node getChild(Node parent, String name) {
    if (parent == null) {
        return null;
    }/* w  w  w .j  av a  2  s .c  o m*/
    Node first = parent.getFirstChild();
    if (first == null) {
        return null;
    }

    for (Node node = first; node != null; node = node.getNextSibling()) {
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (name != null && name.equals(node.getNodeName())) {
            return node;
        }
        if (name == null) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns a list of all child nodes/*from  ww  w  . j  a  va  2  s  .c o m*/
 * @param node
 * @return
 */
public static ArrayList<Node> getAllChildren(Node node) {
    ArrayList<Node> list = new ArrayList<Node>();
    if (node == null)
        return list;

    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        list.add(child);
    }

    return list;
}