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.NodeList;

public class Main {
    public static String FindElementText(Element parent_element, String element_name, String node_name) {
        Element match = FindElement(parent_element, element_name);
        if (match == null)
            return null;
        return GetElementText(match, node_name);
    }

    public static Element FindElement(Element parent_element, String element_name) {
        return FindElement(parent_element, element_name, null);
    }

    public static Element FindElement(Element parent_element, String element_name, String child_element_name) {
        NodeList match = parent_element.getElementsByTagName(element_name);
        if (match.getLength() == 0)
            return null;
        Element m = (Element) match.item(0);
        if (child_element_name != null)
            m = FindElement(m, child_element_name, null);
        return m;
    }

    public static String GetElementText(Element parent, String node_name) {
        NodeList match = parent.getElementsByTagName(node_name);
        if (match.getLength() == 0)
            return null;
        Node m = match.item(0);
        return getNodeText(m);
    }

    public static String getNodeText(Node node) {
        if (node == null)
            return null;
        StringBuffer buff = new StringBuffer();
        for (int c = 0; c < node.getChildNodes().getLength(); c++) {
            Node cn = node.getChildNodes().item(c);
            if (cn.getNodeType() == Node.TEXT_NODE || cn.getNodeType() == Node.CDATA_SECTION_NODE) {
                buff.append(cn.getNodeValue());
            }
        }
        return buff.toString().trim();

    }
}