A method to get the value of desire node from xml document - Java XML

Java examples for XML:DOM Node Value

Description

A method to get the value of desire node from xml document

Demo Code


//package com.java2s;

import org.w3c.dom.Node;

public class Main {
    /**//from  w ww  .  j ava 2  s. co m
     * A method to get the value of desire node from xml document
     * @param Node parent, xml's node object to get
     * @return String , value from provided node
     */
    static public String getNodeValue(Node parent) {
        String ret = "";

        Node n = parent.getFirstChild();
        while (n != null) {
            if (n.getNodeType() == Node.TEXT_NODE) {
                try {
                    ret = n.getNodeValue().trim();
                } catch (NullPointerException ex) {
                    ret = "";
                    break;
                }
            }
            n = n.getNextSibling();
        }
        return (ret);
    }
}

Related Tutorials