Friday, September 18, 2009

Extract variable from XML String - java Blackberry

XML s are basically organize as <tagname>contentent</tagname>

some times we need to get the content of the tag.but in blackberry there no specified method to do that.so use this methods.what we have to do is pass xml string and parameter nane to gettag method then you will get the content between tags

public static String getTag(String XML,String TagName)
{
    try {
        String ans = "error";
        String front1 = "<" + TagName;
        String back1 = "=";
        String[] sssnew = split(XML, front1);
        if (sssnew.length > 1) {
            String[] sss1new = split(sssnew[1], back1);
            if ((sss1new[0]).compareTo(" i:nil") < 1) {
                ans = "";
                return ans;
            }
        }
        String front = "<" + TagName + ">";
        String back = "</" + TagName + ">";
        String[] sss = {};
        sss = split(XML, front);
        if (sssnew.length > 1) {
            String[] sss1 = {};
            sss1 = split(sss[1], back);
            ans = sss1[0];
        }
        return ans;
    } catch (Exception e) {
        return "";
    }
}
private static String[] split(String inString, String delimeter)
{
    String[] retAr = new String[0];
    try {
        Vector vec = new Vector();
        int indexA = 0;
        int indexB = inString.indexOf(delimeter);

        while (indexB != -1) {
            if (indexB > indexA)
                vec.addElement(new String(inString
                        .substring(indexA, indexB)));
            indexA = indexB + delimeter.length();
            indexB = inString.indexOf(delimeter, indexA);
        }
        vec.addElement(new String(inString.substring(indexA, inString
                .length())));
        retAr = new String[vec.size()];
        int intIteration = vec.size();
        for (int i = 0; i < intIteration; i++) {
            retAr[i] = vec.elementAt(i).toString();
        }
    } catch (Exception e)
    {

    }
    return retAr;
}

No comments:

Post a Comment