Thursday, 25 August 2011

Validating Xml in andorid

Some times the response ,came from webservice may contains &lt,&gt.

Inthis case You have to convert '&lt' as '<' and '&gt' as '>'.

public static String validateXml(String xml)
    {
        int offset=0;
        StringBuffer temp=new StringBuffer();
        try{
            while(xml.indexOf("&lt;",offset)!=-1||xml.indexOf("&gt;",offset)!=-1&&offset<xml.length()){
                // if(Constants.LOG)Logger.info("Into &lt and & rt");
                if((xml.indexOf("&lt;",offset)<xml.indexOf("&gt;",offset))&&(xml.indexOf("&lt;",offset)!=-1)||((xml.indexOf("&gt;",offset)==-1)&&(xml.indexOf("&lt;",offset)!=-1))){
                // if(Constants.LOG)Logger.info("Converting &lt into <");
                temp.append(xml.substring(offset, xml.indexOf("&lt;",offset)));
                temp.append("<");
                offset=xml.indexOf("&lt;",offset)+"&lt;".length();
        }
        else if((xml.indexOf("&gt;",offset)<xml.indexOf("&lt;",offset)&&(xml.indexOf("&gt;",offset)!=-1))||((xml.indexOf("&lt;",offset)==-1)&&(xml.indexOf("&gt;",offset)!=-1))){
            // if(Constants.LOG)Logger.info("Converting &lt into >");
            temp.append(xml.substring(offset, xml.indexOf("&gt;",offset)));
            temp.append(">");
            offset=xml.indexOf("&gt;",offset)+"&gt;".length();
        }
        // if(Constants.LOG)Logger.info("offset is : "+offset);
        }
        if(offset<xml.length()){
            temp.append(xml.substring(offset));
        }
        }catch (Exception e) {
        // TODO: handle exception
            //if(Constants.LOG)Logger.info("Array out of bound of exception");
        }
            xml=temp.toString();
        return xml;
    }

No comments:

Post a Comment