Java XPath Get getListValue(String targetDoc, List xpathExps, String encoding)

Here you can find the source of getListValue(String targetDoc, List xpathExps, String encoding)

Description

get List Value

License

Open Source License

Declaration

public static HashMap<String, String> getListValue(String targetDoc, List<String> xpathExps, String encoding)
            throws Exception 

Method Source Code


//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;
    }
}

Related

  1. getFullXPath(Node n)
  2. getHtmlXPath()
  3. getInt(Object node, XPathExpression expression)
  4. getLatestVersion(String tempDir, String url, String groupId, String artifactId, String version)
  5. getListValue(Node node, XPathExpression expression)
  6. getMessage(XPathExpressionException e)
  7. GetMobivateSessionId()
  8. getNameBasedXPath(Node n, boolean includeCurrent)
  9. getNewXPath()