/** * Copyright (c) 2010-2016 by the respective copyright holders. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html */ package org.openhab.binding.rwesmarthome.internal.communicator.util; import java.io.IOException; import java.io.StringReader; 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.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * Utility class for XML. * * @author ollie-dev * */ public class XMLUtil { /** * Returns the xml value. * * @param sIn * @param sxpath * @return * @throws ParserConfigurationException * @throws IOException * @throws SAXException * @throws XPathExpressionException */ public static String XPathValueFromString(String sIn, String sxpath) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException { // DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); Document doc = loadXMLFromString(sIn); XPath xPath = XPathFactory.newInstance().newXPath(); // XPath Query for showing all nodes value XPathExpression expr = xPath.compile(sxpath); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; String sReturn = ""; for (int i = 0; i < nodes.getLength(); i++) { sReturn = nodes.item(i).getNodeValue(); } return sReturn; } /** * Loads a string into a xml document object. * * @param xml * @return * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Document loadXMLFromString(String xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); Document doc = builder.parse(is); return doc; } }