Java XPath Get getNodeListAttValAsStringCol(final String xPath, final Node node, final String attrName)

Here you can find the source of getNodeListAttValAsStringCol(final String xPath, final Node node, final String attrName)

Description

get Node List Att Val As String Col

License

Apache License

Declaration

public static ArrayList<String> getNodeListAttValAsStringCol(final String xPath, final Node node,
            final String attrName) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

import javax.xml.namespace.QName;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Element;

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

public class Main {
    public static final String XPATH_FACTORY = "net.sf.saxon.xpath.XPathFactoryImpl";

    public static ArrayList<String> getNodeListAttValAsStringCol(final String xPath, final Node node,
            final String attrName) throws Exception {
        ArrayList<String> retV = new ArrayList<String>();

        NodeList nl = getNodesListXpathNode(xPath, node);
        int l = nl.getLength();
        Element e = null;//from ww w. j a v  a  2s. com
        String val = "";

        for (int i = 0; i < l; i++) {
            e = (Element) nl.item(i);
            if (e.getNodeType() == Node.ELEMENT_NODE) {
                val = e.getAttribute(attrName);
                if (val != null && val.length() > 0) {
                    retV.add(val);
                }
            }
        }
        return retV;
    }

    public static NodeList getNodesListXpathNode(final String xPath, final Node node) throws Exception {
        return (NodeList) getNodesListXpath(xPath, node, "", "", XPathConstants.NODESET);
    }

    /**
     * 
     * @param xPathS
     * @param node
     * @param nsuri
     * @param pre
     * @param returnType
     * @return Return type is one of XPathConstants .BOOLEAN, .NODE, .NODESET,
     *         .NUMBER, .STRING
     * @throws Exception
     */
    public static Object getNodesListXpath(final String xPathS, final Node node, final String nsuri,
            final String pre, final QName returnType) throws Exception {
        Object matches = null;
        System.setProperty("javax.xml.xpath.XPathFactory:" + XPathConstants.DOM_OBJECT_MODEL, XPATH_FACTORY);

        XPathFactory xpathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
        XPath xpath = xpathFactory.newXPath();
        XPathExpression xpe = xpath.compile(xPathS);
        matches = xpe.evaluate(node, returnType);

        return matches;
    }
}

Related

  1. getNodeList(Node node, String path)
  2. getNodeList(Node node, String xpath)
  3. getNodeList(Node xmlNode, String xpathString)
  4. getNodeList(Reader reader, String expression)
  5. getNodeListAsArray(Node doc, String xpath)
  6. getNodes(Node node, String expStr)
  7. getNodesByPath(String path, Element localElement, Document doc)
  8. getNodesByXPath(Document doc, XPathExpression expr)
  9. getNodesByXPath(Element parent, String name)