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

/**
 * Returns a single element having a given tag
 *///from  w  ww  .  ja  va  2  s . c  om
public static Element getSingleElement(Element element, String tagName) {
    Node node = element.getFirstChild();

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

        node = node.getNextSibling();
    }

    return null;
}

From source file:Main.java

/**
 * I take a xml element and the tag name, look for the tag and get
 * the text content/*  w  w w. ja v  a 2 s.c o  m*/
 * i.e for <employee><name>John</name></employee> xml snippet if
 * the Element points to employee node and tagName is 'name' I will return John
 */
public static String getTextValue(Element ele, String tagName) {

    // Look for elements first
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        return el.getFirstChild().getNodeValue();
    }

    // If there are no elements look for an attribute
    return ele.getAttribute(tagName);
}

From source file:Main.java

public static List<String> parseChildNodesAsList(Element current, String name) {

    List<String> result = new ArrayList<String>();

    NodeList nodes = current.getElementsByTagName(name);

    for (int i = 0; i < nodes.getLength(); ++i) {

        Element e = (Element) nodes.item(i);

        Node child = e.getFirstChild();

        if (child == null)
            continue;

        result.add(child.getNodeValue());

    }//from  w w w  .j ava2 s .c  o m

    return result;
}

From source file:Main.java

private static String getValue(Element ele, String tagName) {
    String value = null;/*from www  . j  av a2s  .  c o m*/
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        value = el.getFirstChild().getNodeValue();
    }

    return value;
}

From source file:Main.java

public static Element getFirstChild(Element e) {
    if (e == null)
        return null;
    Node n = e.getFirstChild();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getNextSibling();/*from ww  w  . j a v  a 2s .c  o m*/
    return (Element) n;
}

From source file:Main.java

License:asdf

public static void replacePerson(Document doc, String name, String phone, String email) {
    Element newPersonNode = doc.createElement("person");

    Element nameNode = doc.createElement("name");
    newPersonNode.appendChild(nameNode);
    Text nametextNode = doc.createTextNode(name);
    nameNode.appendChild(nametextNode);//from   w ww.j  av a2 s  . c  om

    Element phoneNode = doc.createElement("phone");
    newPersonNode.appendChild(phoneNode);
    Text phonetextNode = doc.createTextNode(phone);
    phoneNode.appendChild(phonetextNode);

    Element emailNode = doc.createElement("email");
    newPersonNode.appendChild(emailNode);
    Text emailtextNode = doc.createTextNode(email);
    emailNode.appendChild(emailtextNode);

    Element root = doc.getDocumentElement();
    Element oldPersonNode = (Element) root.getFirstChild();
    root.replaceChild(newPersonNode, oldPersonNode);
}

From source file:Main.java

public static Element getChild(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;
        }//w w w . j  a va 2 s .c om
    }
    return null;
}

From source file:Main.java

public static String getValue(Element item, String tag) {
    String textValue = null;//from  ww  w.  j a v a  2s .c  om

    NodeList nodes = item.getElementsByTagName(tag);
    if (nodes != null && nodes.getLength() > 0) {
        Element element = (Element) nodes.item(0);

        Node node = element.getFirstChild();

        if (node != null) {
            textValue = node.getNodeValue();
        }
    }

    return textValue;
}

From source file:Main.java

public static Map<String, String> getTagValueMap(Element ele, String language) {
    Map<String, String> map = new LinkedHashMap<String, String>();
    NodeList children = ele.getChildNodes();
    Node current = null;/* w  w w  .  ja v  a 2  s .  c  o m*/
    int count = children.getLength();
    for (int i = 0; i < count; i++) {
        current = children.item(i);
        if (current.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) current;
            map.put(current.getNodeName(), element.getFirstChild().getNodeValue());
        }
    }
    return map;
}

From source file:Main.java

/**
 * Helper function for quickly retrieving an boolean value of a given
 * XML element./*from www  . j a v a 2 s  .  co  m*/
 * @param ele The document element from which to pull the boolean value.
 * @param tagName The name of the node.
 * @return The boolean value of the given node in the element passed in.
 */
public static boolean getBooleanValue(Element ele, String tagName) {

    boolean boolVal = false;
    NodeList nl = ele.getElementsByTagName(tagName);

    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        boolVal = el.getFirstChild().getNodeValue().equals("true");
    }

    return boolVal;
}