Java XML Element Text getContent(Element element)

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

Description

This method will return the content of this particular element.

License

Open Source License

Declaration

public static String getContent(Element element) 

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  .  j ava2  s.  c  o m*/
     * This method will return the content of this particular <code>element</code>.
     * For example,
     *
     * <pre>
     *    <result>something_1</result>
     * </pre>
     * When the {@link org.w3c.dom.Element} <code>&lt;result&gt;</code> is passed in as
     * argument (<code>element</code> to this method, it returns the content of it,
     * namely, <code>something_1</code> in the example above.
     * 
     * @return
     */
    public static String getContent(Element element) {
        StringBuffer paramValue = new StringBuffer();
        NodeList childNodes = element.getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++) {
            Node currentNode = childNodes.item(j);
            if (currentNode != null && currentNode.getNodeType() == Node.TEXT_NODE) {
                String val = currentNode.getNodeValue();
                if (val != null) {
                    paramValue.append(val.trim());
                }
            }
        }
        String val = paramValue.toString().trim();
        return val;
    }
}

Related

  1. getContent(Element e)
  2. getContent(Element element)
  3. getContent(Element element)
  4. getContentFromElement(Element element, String namespaceURI, String localName)
  5. getContentText(Element element)
  6. getContentText(Element elementNode)