Java XML Element Value getElementValue(Element root, String elemName)

Here you can find the source of getElementValue(Element root, String elemName)

Description

get the value of an Element in the Xml Document.

License

Open Source License

Parameter

Parameter Description
root the root Element.
elemName the name of the element to search for.

Return

String the element value or null if not found.

Declaration

public static String getElementValue(Element root, String elemName) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Element;

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

public class Main {
    /**/* w w w.  ja v a2 s  . com*/
     * get the value of an Element in the Xml Document.
     * finds the first occurance of the element and retrieves its value.
     * @param root the root Element.
     * @param elemName the name of the element to search for.
     * @return String the element value or null if not found.
     */
    public static String getElementValue(Element root, String elemName) {
        NodeList nl = root.getElementsByTagName(elemName);
        if (null == nl) {
            return (null);
        }
        Node n = nl.item(0);
        return (n.getFirstChild().getNodeValue().trim());
    }
}

Related

  1. getElementValue(Element element, String name)
  2. getElementValue(Element element, String tag)
  3. getElementValue(Element element, String tagName)
  4. getElementValue(Element elm)
  5. getElementValue(Element p_element)
  6. getElementValue(final Element e)
  7. getElementValue(final Element target)