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 String[] getMultiValue(String xmlStr, String xpathStr)
            throws XPathExpressionException, SAXException, IOException {
        if (xmlStr == null)
            return null;
        InputStream is = new ByteArrayInputStream(xmlStr.getBytes("utf-8"));
        return getMultiValue(is, xpathStr);
    }

    /***
     * 
     * @param is
     * @param xpathStr
     * @return
     * if is is null , return null<br>
     * @throws SAXException
     * @throws IOException
     * @throws XPathExpressionException
     */
    public static String[] getMultiValue(InputStream is, String xpathStr)
            throws SAXException, IOException, XPathExpressionException {
        String[] 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 String[nodes.getLength()];
            for (int i = 0; i < nodes.getLength(); i++)
                result[i] = nodes.item(i).getNodeValue();
        }
        return result;
    }
}