Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;

import org.w3c.dom.Node;

public class Main {
    public static boolean isChildNodeExist(Document doc, String parentTag, String childnode) {
        return getChildNode(doc, parentTag, childnode) != null;
    }

    public static Node getChildNode(Document doc, String parentTag, String childnode) {
        try {
            XPath xpath = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xpath.compile("//" + parentTag + "/" + childnode);
            Object obj = expr.evaluate(doc, XPathConstants.NODE);
            return obj != null ? (Node) obj : null;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}