Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.Text;

public class Main {

    public static String getChildNodeValueOf(Node node, String tagName) {
        if (tagName.equals(node.getNodeName())) {
            return getValueOf(node);
        }
        for (Node temp = node.getFirstChild(); temp != null; temp = temp.getNextSibling()) {
            if (temp.getNodeType() == Node.ELEMENT_NODE && tagName.equals(temp.getNodeName())) {
                return getValueOf(temp);
            }
        }
        return null;
    }

    public static final String getValueOf(Node node) {
        if (node == null) {
            return null;
        } else if (node instanceof Text) {
            return node.getNodeValue().trim();
        } else if (node instanceof Element) {
            ((Element) node).normalize();
            Node temp = node.getFirstChild();
            if (temp != null && (temp instanceof Text))
                return temp.getNodeValue().trim();
            else
                return "";
        } else {
            return node.getNodeValue().trim();
        }
    }
}