Java XML Node Value getNodeValue(Node N)

Here you can find the source of getNodeValue(Node N)

Description

Will find the value using getNodeValue or check for one CDATA child

License

Open Source License

Parameter

Parameter Description
N The Node

Return

The value of the Node.

Declaration

public static String getNodeValue(Node N) 

Method Source Code

//package com.java2s;
/* //from  www  .  j ava 2  s  .  co  m
 * File: UtilSm.java
 *
 * Copyright (C) 2010, Ruth Mikkelson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact : Ruth Mikkelson <mikkelsonr@uwstout.edu>
 *           Department of Mathematics, Statistics and Computer Science
 *           University of Wisconsin-Stout
 *           Menomonie, WI 54751, USA
 *
 * This work was supported by the Spallation Neutron Source Division
 * of Oak Ridge National Laboratory, Oak Ridge, TN, USA.
 *
 *  Last Modified:
 * 
 *  $Author:$
 *  $Date:$            
 *  $Rev:$
 */

import java.util.*;

import org.w3c.dom.*;

public class Main {
    /**
     * Will find the value using getNodeValue or check for one CDATA child
     * 
     * @param N  The Node
     * 
     * @return    The value of the Node.
     */
    public static String getNodeValue(Node N) {
        if (N == null)
            return null;

        if (N.getFirstChild() == null)
            return N.getNodeValue();

        if (N.getNodeValue() != null)
            return N.getNodeValue();

        Node[] Nds = SearchChildrenNodes(N, "#cdata-section", null);

        if (Nds == null || Nds.length > 1)
            return null;

        return Nds[0].getNodeValue();
    }

    /**
     * Returns the list of all children nodes of a given node satisfying the conditions.
     * If the conditions are absent( null) all matches will be returned
     * 
     * @param parentNode  The Node whose children are to be searched
     * @param nodeName    The xml name of the node. If null all nodes will be considered.
     * @param attributes  Vector of key and value pairs for match. If an element contains a third item
     *                    it will describe * type matching info.
     * @return the list of all children nodes of a given node satisfying the conditions.
     */
    public static Node[] SearchChildrenNodes(Node parentNode, String nodeName, Vector<String[]> attributes) {
        Vector<Node> children = new Vector<Node>();

        for (Node N = NextChildNode(parentNode, null, nodeName, attributes); N != null; N = NextChildNode(
                parentNode, N, nodeName, attributes))

            children.add(N);

        return children.toArray(new Node[0]);
    }

    /**
     * Uses NextSibling 
     * @param parentNode  The Node whose children are to be searched
     * 
     * @param thisNode    null for first child, otherwise the last returned child of the parent.
     * 
     * @param nodeName    The xml name of the node. If null all nodes will be considered.
     * 
     * @param attributes  Vector of key and value pairs for match. If an element contains a third item
     *                    it will describe * type matching info. Regex expression matching
     *                    
     * @return      The next node matching the given criteria
     */
    public static Node NextChildNode(Node parentNode, Node thisNode, String nodeName, Vector<String[]> attributes) {
        if (parentNode == null)
            return null;

        if (thisNode == null)
            thisNode = parentNode.getFirstChild();
        else
            thisNode = thisNode.getNextSibling();

        if (thisNode == null)
            return thisNode;

        for (Node newNode = thisNode; newNode != null; newNode = newNode.getNextSibling()) {
            if (Match(newNode, nodeName, attributes))
                return newNode;
        }

        return null;

    }

    /**
     * Used to create the Vector of attribute keys and corresponding values in
     * one statement.
     * 
     * @param V    The current Vector or null( an empty vector will be created)
     * 
     * @param S1   The attribute name
     * 
     * @param S2   The corresponding attribute value(or null means the attribute
     *                must be absent)
     *                
     * @param S3   Used to describe * type matching. Not used yet.Can be null.
     * 
     * @return     The new vector with the new information appended to it.
     */
    public static Vector<String[]> Add(Vector<String[]> V, String S1, String S2, String S3) {
        if (V == null)
            V = new Vector<String[]>();

        int n = 1;

        if (S1 == null)
            return V;

        if (S2 != null)
            n++;

        if (S3 != null && S2 != null)
            n++;

        String[] S = new String[n];

        S[0] = S1;
        if (n > 1)
            S[1] = S2;

        if (n > 2)
            S[2] = S3;

        V.addElement(S);

        return V;
    }

    /**
     * Currently checks node for exact matching only
     * 
     * @param node       The node to check for a match to the other conditions
     * 
     * @param nodeName   The nodeName to match( null means they all match)
     * 
     * @param attributes The attributes and corresponding values to match
     * 
     * @return           true if there is a match, otherwise false.
     */
    public static boolean Match(Node node, String nodeName, Vector<String[]> attributes) {
        if (node == null)
            return false;

        if (nodeName != null)
            if (!nodeName.equals(node.getNodeName()))
                return false;

        if (attributes == null || attributes.size() < 1)
            return true;

        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attributes.size(); i++) {
            String[] attr = attributes.elementAt(i);

            if (attr != null && attr.length > 1) {
                Node N = attrs.getNamedItem(attr[0]);
                if (N == null)
                    return false;

                String S = N.getNodeValue();

                if (S == null && (attr.length < 2 || attr[1] == null)) {

                } else if (S == null && attr[1] != null)
                    return false;

                else if (attr[1] != null)
                    if (!attr[1].trim().equals(S.trim()))
                        return false;
            }
        }
        return true;
    }
}

Related

  1. getNodeValue(Node aNode)
  2. getNodeValue(Node fatherNode, String nodeName)
  3. getNodeValue(Node iNode)
  4. getNodeValue(Node iNode)
  5. getNodeValue(Node n)
  6. getNodeValue(Node n)
  7. getNodeValue(Node node)
  8. getNodeValue(Node node)
  9. getNodeValue(Node node)