Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Main {
    private static DocumentBuilder dombuilder = null;
    private static XPath xpath = null;

    public static boolean nodeExist(InputStream is, String xpathStr)
            throws SAXException, IOException, XPathExpressionException {
        boolean result = false;
        Document doc = null;
        if (is == null)
            return result;
        doc = dombuilder.parse(is);
        Object obj = xpath.compile(xpathStr).evaluate(doc, XPathConstants.NODESET);
        if (obj != null) {
            NodeList nodes = (NodeList) obj;
            if (nodes.getLength() > 0) {
                result = true;
            }
        }
        return result;
    }

    public static boolean nodeExist(String xmlStr, String xpathStr)
            throws SAXException, IOException, XPathExpressionException {
        InputStream is = new ByteArrayInputStream(xmlStr.getBytes("utf-8"));
        return nodeExist(is, xpathStr);
    }
}