Java XML Node Text Value getTextContent(org.w3c.dom.Node element)

Here you can find the source of getTextContent(org.w3c.dom.Node element)

Description

return the text content of an element

License

Open Source License

Declaration

public static String getTextContent(org.w3c.dom.Node element) 

Method Source Code

//package com.java2s;
/*/*from  www.j av a 2 s  .  c o m*/
 * Helma License Notice
 *
 * The contents of this file are subject to the Helma License
 * Version 2.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://adele.helma.org/download/helma/license.txt
 *
 * Copyright 1998-2003 Helma Software. All Rights Reserved.
 *
 * $RCSfile$
 * $Author$
 * $Revision$
 * $Date$
 */

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

public class Main {
    /**
     * return the text content of an element
     */
    public static String getTextContent(org.w3c.dom.Node element) {
        StringBuffer childtext = new StringBuffer();
        NodeList childlist = element.getChildNodes();
        int ct = childlist.getLength();

        for (int j = 0; j < ct; j++) {
            org.w3c.dom.Node childNode = childlist.item(j);

            if ((childNode.getNodeType() == Node.TEXT_NODE)
                    || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
                childtext.append(childNode.getNodeValue().trim());
            }
        }

        return childtext.toString();
    }
}

Related

  1. getTextContent(Node node)
  2. getTextContent(Node node)
  3. getTextContent(Node node)
  4. getTextContent(Node node, String defaultValue)
  5. getTextContent(Node node, String path)
  6. getTextContentOld(final Node node)
  7. getTextContents(Node node)
  8. getTextData(Node element)
  9. getTextData(Node node)