Java XML Element Get Value getText(Element elem, String name)

Here you can find the source of getText(Element elem, String name)

Description

get Text

License

Open Source License

Declaration

public static String getText(Element elem, String name) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {

    public static String getText(Element elem, String name) {
        Element childElement = getFirstChildElement(elem, name);
        if (childElement == null) {
            return "";
        }/*from   www .j  av a2  s.c om*/
        Node node = childElement.getFirstChild();
        return node == null ? "" : node.getTextContent();
    }

    public static Element getFirstChildElement(Element elem, String name) {
        NodeList nodeList = elem.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node child = nodeList.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(name)) {
                return (Element) child;
            }
        }
        return null;
    }
}

Related

  1. getText(Element elem)
  2. getText(Element elem)
  3. getText(Element elem)
  4. getText(Element elem)
  5. getText(Element elem, String ifnull)
  6. getText(Element element)
  7. getText(Element element)
  8. getText(Element element)
  9. getText(Element element)