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.nio.charset.Charset;
import java.util.ArrayList;

import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

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

import org.w3c.dom.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import org.xml.sax.SAXException;

public class Main {
    private final static Charset CHARSET_UTF8 = Charset.forName("UTF-8");

    /**
     *
     * @param xmlContent
     * @param expression
     * @return
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws XPathExpressionException
     */
    public static List<String> getValues(String xmlContent, String expression)
            throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {

        return getValues(xmlContent, CHARSET_UTF8, expression);
    }

    /**
     *
     * @param xmlContent
     * @param charset
     * @param expression
     * @return
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws XPathExpressionException
     */
    public static List<String> getValues(String xmlContent, Charset charset, String expression)
            throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {

        List<String> valueList = new ArrayList<String>();

        NodeList nodeList = getNodeList(xmlContent, expression);
        int length = nodeList.getLength();
        for (int seq = 0; seq < length; seq++) {
            Node node = nodeList.item(seq);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                NodeList childNodeList = node.getChildNodes();
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < childNodeList.getLength(); i++) {
                    Node childNode = childNodeList.item(i);
                    if (childNode.getNodeType() == Node.TEXT_NODE) {
                        sBuilder.append(((Text) childNode).getNodeValue());
                    }
                }
                valueList.add(sBuilder.toString());

            } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                valueList.add(node.getNodeValue());
            }
        }

        return valueList;
    }

    private static NodeList getNodeList(Document document, String expression) throws XPathExpressionException {

        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xpath.evaluate(expression, document.getDocumentElement(),
                XPathConstants.NODESET);

        return nodeList;
    }

    private static NodeList getNodeList(String xmlContent, String expression)
            throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {

        Document document = parse(xmlContent, CHARSET_UTF8);
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xpath.evaluate(expression, document.getDocumentElement(),
                XPathConstants.NODESET);

        return nodeList;
    }

    /**
     *
     * @param xmlContent
     * @param charset
     * @return
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    private static Document parse(String xmlContent, Charset charset)
            throws SAXException, IOException, ParserConfigurationException {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        documentBuilderFactory.setValidating(false);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes(charset)));

        return document;
    }
}