Java tutorial
//package com.java2s; //License from project: Artistic License import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import java.io.ByteArrayInputStream; import java.io.InputStream; public class Main { public static String extractValue(String xml, String xpathExpression) { String actual; try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder(); byte[] bytes = xml.getBytes("UTF-8"); InputStream inputStream = new ByteArrayInputStream(bytes); Document doc = docBuilder.parse(inputStream); XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); actual = xpath.evaluate(xpathExpression, doc, XPathConstants.STRING).toString(); } catch (Exception e) { throw new RuntimeException(e); } return actual; } }