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

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

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

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

    /***
     * 
     * @param is
     * @param xpathStr
     * @return
     * if is is null , return null<br>
     * only the fist value will be returned
     * @throws SAXException
     * @throws IOException
     * @throws XPathExpressionException
     */
    public static String getSingleValue(InputStream is, String xpathStr)
            throws SAXException, IOException, XPathExpressionException {
        String result = null;
        Document doc = null;
        if (is == null)
            return result;
        if (xpathStr == null || xpathStr.trim().length() == 0)
            return result;
        doc = dombuilder.parse(is);
        //"//db/@dbType"
        result = xpath.compile(xpathStr).evaluate(doc);
        return result;
    }
}