Java XML Element Get getElementContents(Element element, String elementName)

Here you can find the source of getElementContents(Element element, String elementName)

Description

get Element Contents

License

Open Source License

Declaration

public static String getElementContents(Element element, String elementName) throws IOException 

Method Source Code


//package com.java2s;
/*//from w w w.  j a  v a  2s. co  m
 * XmlUtil.java
 *
 * (c) 2009  The Echo Nest
 * See "license.txt" for terms
 *
 *
 */

import java.io.IOException;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static String getElementContents(Element element, String elementName) throws IOException {
        Element first = getFirstElement(element, elementName);
        if (first != null) {
            return first.getTextContent().trim();
        } else {
            return null;
        }
    }

    public static Element getFirstElement(Element element, String elementName) throws IOException {
        NodeList list = element.getElementsByTagName(elementName);
        if (list.getLength() >= 1) {
            Element subElement = (Element) list.item(0);
            return subElement;
        } else {
            return null;
        }
    }
}

Related

  1. getElementContent(Element element, String defaultStr)
  2. getElementContent(Element element, String defaultStr)
  3. getElementContent(final Element el)
  4. getElementContent(final Element element)
  5. getElementContent(final Element element, final boolean trim)
  6. getElementDescendant(Element elem, String ns_uri, String tagName)
  7. getElementForLine(Document document, int line)
  8. getElementId(Element element)
  9. getElementIndex(Element ele)