How can I prevent text that contains "" or ">" from interfering with parsing. Are there other characters I need to be concerned about?

Robert Quinn

A simple XML "encode" should do the trick.

  /**
   * Escape the "special" characters as required for inclusion in XML elements
   * Replaces all incidences of 
   *	& with & 
	*  < with < 
	*  > with > 
	*  " with "  
	*  ' with &apos;
   * @param s The string to scan
   * @return String
   */
  public static String escapeStringForXML(String s) {
    char[] array = s.toCharArray();
    StringBuffer sb = new StringBuffer();
    for(int i=0; i < array.length; i++) {
       switch(array[i])
       {
          case '&':
             sb.append("&");
             break;
          case '<':
             sb.append("<");
             break;
          case '>':
             sb.append(">");
             break;
          case '"':
             sb.append(""");
             break;
          case ''':
             sb.append("&apos;");
             break;
          default:
             sb.append(array[i]);
       }      
    }
    return sb.toString();
  }
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact