Here you can find the source of xmlEncode(String s)
Parameter | Description |
---|---|
s | any String |
public static String xmlEncode(String s)
//package com.java2s; /**/*from w w w . j a va2 s .com*/ * Copyright 2006 OCLC Online Computer Library Center 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 { /** * XML encode a string. * @param s any String * @return the String with &, <, and > encoded for use in XML. */ public static String xmlEncode(String s) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length(); ++i) { char c = s.charAt(i); switch (c) { case '&': sb.append("&"); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '"': sb.append("""); break; case '\'': sb.append("'"); break; default: sb.append(c); break; } } return sb.toString(); } }