Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.PrintWriter; public class Main { /** * Write attribute. If the value is null, no attribute is written. * * @param out * The writer. * @param name * The attribute name. * @param value * The attribute value. */ public static void writeAttribute(PrintWriter out, String name, String value) { if (value != null) { out.print(' '); out.print(name); out.print("=\""); writeCharacters(out, value); out.print("\""); } } /** * Write characters. Character data is properly escaped. * * @param out * The writer. * @param value * The text as a strng. */ public static void writeCharacters(PrintWriter out, String value) { writeCharacters(out, value.toCharArray(), 0, value.length()); } /** * Write characters. Character data is properly escaped. * * @param out * The writer. * @param ch * The character array. * @param start * Start position. * @param length * Substring length. */ public static void writeCharacters(PrintWriter out, char[] ch, int start, int length) { for (int j = start; j < start + length; j++) { char c = ch[j]; if (c == '&') { out.print("&"); } else if (c == '"') { out.print("""); } else if (c == '<') { out.print("<"); } else if (c == '>') { out.print(">"); } else if (c == '\'') { out.print("'"); } else { out.print(c); } } } }