Get a valid xPath string to enable retrieval of an Element by AND ing attributes name and their corresponding value. - Java XML

Java examples for XML:XML Attribute

Description

Get a valid xPath string to enable retrieval of an Element by AND ing attributes name and their corresponding value.

Demo Code

/*/*from   www .  j  av  a  2 s  . co m*/
 * Copyright 2005-2010 Ignis Software Tools Ltd. All rights reserved.
 */
//package com.java2s;

public class Main {
    /**
     * 
     * Get a valid xPath string to enable retrieval of an Element by ANDing
     * attributes name and their corresponding value. note that the full tag
     * path must also be stated
     * 
     * @param tagPath
     *            example: /calls/step
     * @param attrNames
     *            example: call_step
     * @param attrValues
     *            example: Call failed
     * 
     * @return A valid xPath string
     * 
     * 
     */
    public static String getXpathAND(String tagPath, String[] attrNames,
            String[] attrValues) {
        StringBuffer buf = new StringBuffer();
        String retStr;

        buf.append(tagPath);
        buf.append("[");
        for (int i = 0; i < attrValues.length; i++) {
            buf.append("@");
            buf.append(attrNames[i]);
            buf.append("=");
            buf.append("'");
            buf.append(attrValues[i]);
            buf.append("'");
            if (i != (attrValues.length - 1))
                buf.append(" and ");
        }
        buf.append("]");

        System.out.println("Xpath generated: " + (retStr = buf.toString()));

        return retStr;
    }
}

Related Tutorials