Here you can find the source of xmlEncode(String s)
public static String xmlEncode(String s)
//package com.java2s; /*/* w ww .j av a2s .com*/ Copyright 2006 OCLC Online Computer Library Center, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ public class Main { public static String xmlEncode(String s) { boolean changed = false; char c; StringBuffer sb = null; for (int i = 0; i < s.length(); i++) { c = s.charAt(i); if (c < 0xa) { if (!changed) { if (i > 0) sb = new StringBuffer(s.substring(0, i)); else sb = new StringBuffer(); changed = true; } sb.append("&#x").append(Integer.toHexString(c)).append(';'); } else if (c == '<') { if (!changed) { if (i > 0) sb = new StringBuffer(s.substring(0, i)); else sb = new StringBuffer(); changed = true; } sb.append("<"); } else if (c == '>') { if (!changed) { if (i > 0) sb = new StringBuffer(s.substring(0, i)); else sb = new StringBuffer(); changed = true; } sb.append(">"); } else if (c == '"') { if (!changed) { if (i > 0) sb = new StringBuffer(s.substring(0, i)); else sb = new StringBuffer(); changed = true; } sb.append("""); } else if (c == '&') { if (!changed) { if (i > 0) sb = new StringBuffer(s.substring(0, i)); else sb = new StringBuffer(); changed = true; } sb.append("&"); } else if (c == '\'') { if (!changed) { if (i > 0) sb = new StringBuffer(s.substring(0, i)); else sb = new StringBuffer(); changed = true; } sb.append("'"); } else if (changed) sb.append(c); } if (!changed) return s; return sb.toString(); } }