Java XML Element Get Value getTextContentOfElement(Element elem, String tagName, boolean required)

Here you can find the source of getTextContentOfElement(Element elem, String tagName, boolean required)

Description

get Text Content Of Element

License

Apache License

Declaration

public static String getTextContentOfElement(Element elem, String tagName, boolean required)
            throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.util.ArrayList;

import java.util.List;

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

public class Main {
    public static String getTextContentOfElement(Element elem, String tagName, boolean required)
            throws IOException {
        List<String> result = getTextContentOfElements(elem, tagName);
        if (result.isEmpty() && !required)
            return null;
        if (result.size() != 1)
            throw new IOException("expected exactly one " + tagName + " element");
        return result.get(0);
    }/*from  w  w w . j a v  a  2  s.  c  om*/

    public static List<String> getTextContentOfElements(Element elem, String tagName) {
        List<String> result = new ArrayList<String>();
        NodeList inputElems = elem.getElementsByTagName(tagName);
        for (int i = 0; i < inputElems.getLength(); i++) {
            Element childElem = (Element) inputElems.item(i);
            result.add(childElem.getTextContent());
        }
        return result;
    }
}

Related

  1. getTextContent(Element element)
  2. getTextContent(Element element)
  3. getTextContent(Element element)
  4. getTextContent(Element element)
  5. getTextContentFromFirstElementByTagName(Element element, String tagName)
  6. getTextContentOfElements(Element elem, String tagName)
  7. getTextContents(Element e)
  8. getTextData(Element element)
  9. getTextElementValue(Element ele)