Java XML Element Get Value getTextContent(Element element)

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

Description

Returns the value of any text nodes stored as children of the given element

License

Open Source License

Parameter

Parameter Description
element the element to check for text content

Exception

Parameter Description
DOMException an exception

Return

string containing text content of element or empty string

Declaration

static String getTextContent(Element element) throws DOMException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from w w  w  .  ja va 2 s . c o m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import org.w3c.dom.*;

public class Main {
    /**
     * Returns the value of any text nodes stored as children of the given element
     * @param element the element to check for text content
     * @return string containing text content of element or empty string
     * @throws DOMException
     */
    static String getTextContent(Element element) throws DOMException {
        NodeList children = element.getChildNodes();
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < children.getLength(); ++i) {
            Node currentNode = children.item(i);
            if (currentNode.getNodeType() == Node.TEXT_NODE) {
                result.append(currentNode.getNodeValue());
            }
        }
        return result.toString();
    }
}

Related

  1. getTextContent(Element e)
  2. getTextContent(Element e, String name)
  3. getTextContent(Element el)
  4. getTextContent(Element ele)
  5. getTextContent(Element element)
  6. getTextContent(Element element)
  7. getTextContent(Element element)
  8. getTextContent(Element element)
  9. getTextContent(Element element)