Example usage for org.w3c.dom Element getFirstChild

List of usage examples for org.w3c.dom Element getFirstChild

Introduction

In this page you can find the example usage for org.w3c.dom Element getFirstChild.

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:Main.java

/**
 * Recursively removes all text nodes from a DOM element.
 * /*from w  w w . jav a  2  s.  c o  m*/
 * @param element
 *        The element
 */
public static void removeTextNodes(Element element) {
    Node nextNode = element.getFirstChild();
    for (Node child = element.getFirstChild(); nextNode != null;) {
        child = nextNode;
        nextNode = child.getNextSibling();
        if (child.getNodeType() == Node.TEXT_NODE)
            element.removeChild(child);
        else if (child.getNodeType() == Node.ELEMENT_NODE)
            removeTextNodes((Element) child);
    }
}

From source file:Main.java

static public String getElementText(Element elm) {
    Node node = elm.getFirstChild();
    if (node != null && node.getNodeType() == Node.TEXT_NODE)
        return node.getNodeValue();

    return null;//from  w  w w . j  ava 2  s  .c  om
}

From source file:Main.java

public static String getElemText(Element elem) {
    if (elem != null && elem.getFirstChild() != null) {
        return elem.getFirstChild().getNodeValue();
    }/*from w w  w. ja  v a 2s  . c o  m*/
    return null;
}

From source file:Main.java

/**
 * Find child element by name//from   w w w  .  j av  a 2s  .c o  m
 * @param element parent element
 * @param childName child name
 * @return 
 */
public static Element findChildElement(Element element, String childName) {
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof Element && childName.equals(child.getNodeName())) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns a list of elements having a given tag
 *///from ww w .ja v  a2 s  .  c om
public static List<Element> getElements(Element element, String tagName) {
    Node node = element.getFirstChild();
    List<Element> elements = new ArrayList<Element>();

    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo(tagName) == 0)
            elements.add((Element) node);

        node = node.getNextSibling();
    }

    return elements;
}

From source file:Main.java

public static boolean hasChildren(Element e) {
    if (e == null)
        return false;
    return e.getFirstChild() != null;
}

From source file:Main.java

public static Element getChildElementByTagName(Element parentElement, String childTag) {
    for (Node temp = parentElement.getFirstChild(); temp != null; temp = temp.getNextSibling())
        if (temp.getNodeType() == Node.ELEMENT_NODE && childTag.equals(temp.getNodeName())) {
            return (Element) temp;
        }//from   w  ww. j  av a  2  s  .c om
    return null;
}

From source file:Main.java

public static void modifyingTextbyCuttingandPasting(Document doc) {
    Element root = doc.getDocumentElement();
    Element place = (Element) root.getFirstChild();
    Text name = (Text) place.getFirstChild().getFirstChild();
    Text directions = (Text) place.getLastChild().getFirstChild();

    //    name.deleteData(offset,count);
    name.deleteData(2, 3);// w w  w .  j  a  v a 2 s  . c o  m

    //String bridge = directions.substringData(offset,count);
    String bridge = directions.substringData(2, 3);
    name.appendData(bridge);
}

From source file:Main.java

public static Element getChildElement(Element parent, String name) {
    for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof Element && name.equals(child.getNodeName())) {
            return (Element) child;
        }/*from  w  w w.  j av a 2  s . c  o m*/
    }
    return null;
}

From source file:Main.java

public static Element element(Element element) {
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof Element)
            return (Element) child;
    }//from  www  .  ja v  a 2  s .c o  m
    return null;
}