Java XPath Create getXPathValue(XPath xpath, Node d, String xq, String def)

Here you can find the source of getXPathValue(XPath xpath, Node d, String xq, String def)

Description

get X Path Value

License

Open Source License

Declaration

public static String getXPathValue(XPath xpath, Node d, String xq, String def) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

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

public class Main {
    public static String getXPathValue(XPath xpath, Node d, String xq, String def) {
        if (xq.isEmpty()) {
            return def;
        }//from  w w  w .  j a va 2s.co m
        StringBuilder sb = new StringBuilder();
        try {
            NodeList nl = (NodeList) xpath.evaluate(xq, d, XPathConstants.NODESET);
            for (int i = 0; i < nl.getLength(); i++) {
                String s = nl.item(i).getTextContent();
                if (s == null) {
                    continue;
                }
                if (s.trim().isEmpty()) {
                    continue;
                }
                if (sb.length() > 0) {
                    sb.append("; ");
                }
                sb.append(s);
            }
            if (sb.length() == 0) {
                return def;
            }
            return sb.toString();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return def;
    }
}

Related

  1. getXPathNodeIndex(Node node, boolean ignoreWhitespace)
  2. getXPathNodes(XPath xpath, Object inic, String name)
  3. getXPathToContent(final Node root, final String selectedContent)
  4. getXPathType(Class c)
  5. getXpathVal(InputStream pInputStream, String pXPath)
  6. newXPath()
  7. newXPath()
  8. newXpathExpression(final XPath xpath, final String expression)
  9. newXPathFactory()