Java XML Element Get Value getTextTrim(Element element)

Here you can find the source of getTextTrim(Element element)

Description

get Text Trim

License

Apache License

Declaration

public static String getTextTrim(Element element) 

Method Source Code


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

import org.w3c.dom.*;

public class Main {
    public static String getTextTrim(Element element) {
        String txt = getText(element);
        return (txt == null) ? txt : txt.trim();
    }//from   w ww  .  j a  v a 2  s  .  c  om

    public static String getText(Element element) {
        StringBuffer content = new StringBuffer();
        if (element != null) {
            NodeList nodes = element.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++) {
                Node child = nodes.item(i);
                if (isText(child)) {
                    // cast to super class that contains Text and CData
                    content.append(((CharacterData) child).getData());
                }
            }
        }
        return (content.length() == 0) ? null : content.toString();
    }

    private static boolean isText(Node node) {
        int ntype = node.getNodeType();
        return ntype == Node.TEXT_NODE || ntype == Node.CDATA_SECTION_NODE;
    }
}

Related

  1. getTextList(Element elem, String name)
  2. getTextOfElement(Element e)
  3. getTexts(Element root, String elementName)
  4. getTextString(Element e)
  5. getTextTrim(Element element)
  6. getTextValue(Element el, String tagName)
  7. getTextValue(Element ele, String tagName)
  8. getTextValue(Element ele, String tagName)
  9. getTextValue(Element ele, String tagName)