Here you can find the source of getListValue(String targetDoc, List
public static HashMap<String, String> getListValue(String targetDoc, List<String> xpathExps, String encoding) throws Exception
//package com.java2s; //License from project: Open Source License import java.io.ByteArrayInputStream; import java.util.HashMap; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; public class Main { private static DocumentBuilderFactory factory; public static HashMap<String, String> getListValue(String targetDoc, List<String> xpathExps, String encoding) throws Exception { DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(targetDoc.getBytes(encoding))); XPathFactory pathFactory = XPathFactory.newInstance(); XPath xpath = pathFactory.newXPath(); XPathExpression pathExpression; String result;/*from w w w. j a v a2 s .c o m*/ HashMap<String, String> values = new HashMap<String, String>(); for (String xpathExp : xpathExps) { pathExpression = xpath.compile(xpathExp); result = (String) pathExpression.evaluate(doc, XPathConstants.STRING); if (result != null) { result = result.trim(); } values.put(xpathExp, result); } return values; } }