Java XML Has Child hasOnlyTextChildNodes(final Node node)

Here you can find the source of hasOnlyTextChildNodes(final Node node)

Description

Checks if the given node has only text children.

License

Open Source License

Parameter

Parameter Description
node parent.

Return

'TRUE' if the given node has only text children; 'FALSE' otherwise.

Declaration

public static boolean hasOnlyTextChildNodes(final Node node) 

Method Source Code

//package com.java2s;
/**/*w  ww  . j a va2  s .  c  om*/
 * Copyright ? Microsoft Open Technologies, Inc.
 *
 * All Rights Reserved
 *
 * 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
 *
 * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 * ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
 * PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
 *
 * See the Apache License, Version 2.0 for the specific language
 * governing permissions and limitations under the License.
 */

import java.util.ArrayList;

import java.util.List;

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

public class Main {
    /**
     * Checks if the given node has only text children.
     *
     * @param node parent.
     * @return 'TRUE' if the given node has only text children; 'FALSE' otherwise.
     */
    public static boolean hasOnlyTextChildNodes(final Node node) {
        boolean result = true;
        final NodeList children = node.getChildNodes();
        for (int i = 0; result && i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (child.getNodeType() != Node.TEXT_NODE) {
                result = false;
            }
        }

        return result;
    }

    /**
     * Gets the given node's children of the given type.
     *
     * @param node parent.
     * @param nodetype searched child type.
     * @return children.
     */
    public static List<Node> getChildNodes(final Node node, final short nodetype) {
        final List<Node> result = new ArrayList<Node>();

        final NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (child.getNodeType() == nodetype) {
                result.add(child);
            }
        }

        return result;
    }
}

Related

  1. hasElementChildren(Element element)
  2. hasElementChildren(Element elemNode)
  3. hasElementChildren(Element elemNode)
  4. hasNamedChild(Element e, String name)
  5. hasNonWhitespaceChildren(Element element)
  6. hasOnlyTextChildren( Node node)
  7. hasTextChildNodesOnly(Node node)