Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

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.Node;
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 Node[] getMultiNodeFromFile(String fileName, String xpathStr)
            throws SAXException, IOException, XPathExpressionException {
        InputStream is = new FileInputStream(new File(fileName));
        return getMultiNode(is, xpathStr);
    }

    public static Node[] getMultiNode(InputStream is, String xpathStr)
            throws SAXException, IOException, XPathExpressionException {
        Node[] result = null;
        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;
            result = new Node[nodes.getLength()];
            for (int i = 0; i < nodes.getLength(); i++)
                result[i] = nodes.item(i);
        }
        return result;
    }
}