Java XML Element Get Value getTrimmedTextValue(Element valueEle)

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

Description

Retruns the trimmed text value of the DOM element.

License

Apache License

Declaration

public static String getTrimmedTextValue(Element valueEle) 

Method Source Code

//package com.java2s;
/*//from  ww  w.  j ava2s  .  co  m
 * Copyright 2004-2009 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.Comment;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Retruns the trimmed text value of the DOM element.
     *
     * @see #getTextValue(org.w3c.dom.Element)
     */
    public static String getTrimmedTextValue(Element valueEle) {
        return getTextValue(valueEle).trim();
    }

    /**
     * Extract 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.
     *
     * @see org.w3c.dom.CharacterData
     * @see org.w3c.dom.EntityReference
     * @see org.w3c.dom.Comment
     */
    public static String getTextValue(Element valueEle) {
        StringBuffer value = new StringBuffer();
        NodeList nl = valueEle.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node item = nl.item(i);
            if ((item instanceof org.w3c.dom.CharacterData && !(item instanceof Comment))
                    || item instanceof EntityReference) {
                value.append(item.getNodeValue());
            }
        }
        return value.toString();
    }
}

Related

  1. getTextValue(final Element element, final String tagName)
  2. getTextWithoutTrim(Element elem)
  3. getTrimmedText(Element element)
  4. getTrimmedTextContent(Element element)
  5. getTrimmedTextContent(Element element)
  6. getType(Element elementType)
  7. getUniqueElement(Element root, String elementName)
  8. getUniqueSubnode(Element rootNode, String name)
  9. getValue(Element e, String tagName)