Java XML Element Text getElementText(Element elem)

Here you can find the source of getElementText(Element elem)

Description

Get the concatenated value of all Text nodes that are immediate children of the given Element.

License

Apache License

Parameter

Parameter Description
elem Element to examine.

Return

Concatenated text of all child Text nodes. An empty string is returned if there are no child Text nodes or they are all empty or contain only whitespace.

Declaration

public static String getElementText(Element elem) 

Method Source Code

//package com.java2s;
/*// www  . j a  va  2s. co  m
 * Copyright (C) 2014 Dell, Inc.
 * 
 * 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 java.nio.ByteBuffer;
import java.nio.charset.Charset;

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

public class Main {
    /**
     * The Charset object for the UTF-8 character set.
     */
    public static final Charset UTF8_CHARSET = Charset.forName("UTF-8");

    /**
     * Get the concatenated value of all Text nodes that are immediate children of the
     * given Element. If the element has no content, it will not have a child Text node.
     * If it does have content, it will usually have a single child Text node. But in
     * rare cases it could have multiple child Text nodes. If multiple child Text nodes
     * are found, their content is concatenated into a single string, each separated by a
     * single space. The value returned is trimmed of beginning and ending whitespace.
     * If the element has no child Text nodes, or if all child Text nodes are empty or
     * have whitespace-only values, an empty string is returned.
     *
     * @param  elem Element to examine.
     * @return      Concatenated text of all child Text nodes. An empty string is returned
     *              if there are no child Text nodes or they are all empty or contain only
     *              whitespace.
     */
    public static String getElementText(Element elem) {
        StringBuilder result = new StringBuilder();
        NodeList nodeList = elem.getChildNodes();
        for (int index = 0; index < nodeList.getLength(); index++) {
            Node childNode = nodeList.item(index);
            if (childNode != null && (childNode instanceof Text)) {
                result.append(" ");
                result.append(((Text) childNode).getData());
            }
        }
        return result.toString().trim();
    }

    /**
     * Convert the given byte[] to a String using the {@link #UTF8_CHARSET} decoder. This
     * is the inverse of {@link #toBytes(String)}. As with that method, null begets null.
     *
     * @param bytes A byte[] representing a String value.
     * @return      The decoded String value, or null if null is given.
     */
    public static String toString(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        //optimization for ASCII string
        String ascii = toAsciiString(bytes);
        if (ascii != null)
            return ascii;

        return new String(bytes, UTF8_CHARSET);
    }

    /**
     * Extract the byte[] within the given ByteBuffer and decode into a String using UTF-8.
     * This method calls {@link #copyBytes(ByteBuffer)}, which examines the ByteBuffer
     * without side-effects, therefore allowing it to be read again.
     * 
     * @param bytes ByteBuffer object.
     * @return      Internal byte[] value converted to a String using UTF-8.
     */
    public static String toString(ByteBuffer bytes) {
        return toString(copyBytes(bytes));
    }

    /**
     * Convert the a subset of given byte[] starting at index 'offset' for 'length' bytes
     * to a String using the reverse process used by {@link #toBytes(String)}. As with
     * that method, null begets null.
     *
     * @param bytes     Byte[] to convert.
     * @param offset    Index of first byte to convert.
     * @param length    Number of bytes to convert.
     * @return          Decoded string, or null if null is given.
     */
    public static String toString(byte[] bytes, int offset, int length) {
        if (bytes == null) {
            return null;
        }
        //optimization for ASCII string
        String ascii = toAsciiString(bytes, offset, length);
        if (ascii != null)
            return ascii;

        return new String(bytes, offset, length, UTF8_CHARSET);
    }

    private static String toAsciiString(byte[] bytes) {
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] < 0)
                return null;
        }
        char[] chars = new char[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            chars[i] = (char) bytes[i];
        }
        return new String(chars);
    }

    private static String toAsciiString(byte[] bytes, int offset, int length) {
        for (int i = 0; i < length; i++) {
            if (bytes[offset + i] < 0)
                return null;
        }
        char[] chars = new char[length];
        for (int i = 0; i < length; i++) {
            chars[i] = (char) bytes[offset + i];
        }
        return new String(chars);
    }

    /**
     * Extract the bytes in the given ByteBuffer and return it as a byte[] without
     * affecting the mark, position, or limit of the given buffer. This method should be
     * used instead of {@link #getBytes(ByteBuffer)} when the ByteBuffer might be re-read
     * again.
     *
     * @param   bytes   ByteBuffer.
     * @return          Contents between 'position' and 'limit' (aka 'remaining') as a
     *                  byte[]. Parameter object is unaffected.
     */
    public static byte[] copyBytes(ByteBuffer bytes) {
        ByteBuffer copy = bytes.duplicate();
        byte[] result = new byte[copy.remaining()]; // bytes between position and limit
        copy.get(result);
        return result;
    }
}

Related

  1. getContent(Element element)
  2. getContent(Element element)
  3. getContentFromElement(Element element, String namespaceURI, String localName)
  4. getContentText(Element element)
  5. getContentText(Element elementNode)
  6. getElementText(Element elem)
  7. getElementText(Element elem, boolean mandatory)
  8. getElementText(Element elem, String name)
  9. getElementText(Element element)