Java Utililty Methods XPath Create

List of utility methods to do XPath Create

Description

The list of methods to do XPath Create are organized into topic(s).

Method

StringgetXPathFromVector(Vector path)
get X Path From Vector
StringBuffer strBuf = new StringBuffer();
int length = path.size();
for (int i = 0; i < length; i++) {
    Node tempNode = (Node) path.elementAt(i);
    short nodeType = getNodeType(tempNode);
    String targetValue = getValue(tempNode, nodeType);
    int position = 1;
    tempNode = getPreviousTypedNode(tempNode, nodeType);
...
String[]getXPathListForNode(Node n)
Returns a list of tag names representing the path from the document root to the given node n.
if (n == null) {
    return EMPTY_STRING_ARRAY;
List<String> ancestors = new ArrayList<String>();
ancestors.add(String.format("%s[%s]", n.getNodeName(), getIndexInParent(n)));
Node parent = n.getParentNode();
while (parent != null) {
    ancestors.add(0, String.format("%s[%s]", parent.getNodeName(), getIndexInParent(parent)));
...
XPathExpressiongetXPathNoCache(String exp)
get X Path No Cache
return sFactory.newXPath().compile(exp);
intgetXPathNodeIndex(Node node, boolean ignoreWhitespace)
Gets Node Index value for the XPath expression

e.g.

int nodeIndex = 0;
if (node == null)
    return -1;
Node prevNode = node;
while ((prevNode = prevNode.getPreviousSibling()) != null)
    if (nodesEqual(node, prevNode, ignoreWhitespace))
        nodeIndex++;
if (nodeIndex > 0)
    nodeIndex++;
if (nodeIndex == 0)
    Node nextNode = node;
    boolean found = false;
    while (((nextNode = nextNode.getNextSibling()) != null) && (!found))
        if (nodesEqual(node, nextNode, ignoreWhitespace))
            nodeIndex++;
            found = true;
return nodeIndex;
NodeListgetXPathNodes(XPath xpath, Object inic, String name)
get X Path Nodes
return (NodeList) xpath.evaluate(name, inic, XPathConstants.NODESET);
StringgetXPathToContent(final Node root, final String selectedContent)
Determines the XPath expression to the selected character data (element content).
NodeList childList = root.getChildNodes();
Node childNode = null;
for (int i = 0, length = childList.getLength(); i < length && xpathExpressionToContent == null; i++) {
    childNode = childList.item(i);
    if (childNode.getNodeType() == Node.TEXT_NODE) {
        if (childNode.getTextContent().equals(selectedContent)) {
            xpathExpressionToContent = getXPathExpression(childNode.getParentNode());
    getXPathToContent(childNode, selectedContent);
return xpathExpressionToContent + "/text()"; 
QNamegetXPathType(Class c)
get X Path Type
if (c == Node.class)
    return XPathConstants.NODE;
if (c == NodeList.class)
    return XPathConstants.NODESET;
if (c == String.class)
    return XPathConstants.STRING;
if (c == Boolean.class)
    return XPathConstants.BOOLEAN;
...
StringgetXpathVal(InputStream pInputStream, String pXPath)
Recover the value associated with a xpath expression
try {
    DocumentBuilder docB = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = docB.parse(pInputStream);
    Node root = doc.getFirstChild();
    XPath xpath = XPathFactory.newInstance().newXPath();
    return xpath.evaluate(pXPath, root);
} catch (ParserConfigurationException pce) {
} catch (NumberFormatException e) {
...
StringgetXPathValue(XPath xpath, Node d, String xq, String def)
get X Path Value
if (xq.isEmpty()) {
    return def;
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();
...
XPathnewXPath()
new X Path
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
return xpath;