Java XML Element Get Value getTextValue(Element valueEle)

Here you can find the source of getTextValue(Element valueEle)

Description

Extracts the text value from the given DOM element, ignoring XML comments.

License

Apache License

Declaration

public static String getTextValue(Element valueEle) 

Method Source Code

//package com.java2s;
/*/* w w w . j a v  a  2s .c o  m*/
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.CharacterData;
import org.w3c.dom.Comment;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.springframework.util.Assert;

public class Main {
    /**
     * Extracts the text value from the given DOM element, ignoring XML comments.
     * <p>Appends all CharacterData nodes and EntityReference nodes into a single
     * String value, excluding Comment nodes. Only exposes actual user-specified
     * text, no default values of any kind.
     * @see CharacterData
     * @see EntityReference
     * @see Comment
     */
    public static String getTextValue(Element valueEle) {
        Assert.notNull(valueEle, "Element must not be null");
        StringBuilder sb = new StringBuilder();
        NodeList nl = valueEle.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node item = nl.item(i);
            if ((item instanceof CharacterData && !(item instanceof Comment))
                    || item instanceof EntityReference) {
                sb.append(item.getNodeValue());
            }
        }
        return sb.toString();
    }
}

Related

  1. getTextValue(Element element, String tagName)
  2. getTextValue(Element element, String tagName)
  3. getTextValue(Element node)
  4. getTextValue(Element valueEle)
  5. getTextValue(Element valueEle)
  6. getTextValue(final Element elem)
  7. getTextValue(final Element element, final String tagName)
  8. getTextWithoutTrim(Element elem)
  9. getTrimmedText(Element element)