Java tutorial
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.io.IOException; public class Main { private static final String XML_LESS_THAN = "<"; private static final String XML_AMPERSAND = "&"; private static final String XML_QUOTE = """; /** Preferred over ' according to http://www.w3.org/TR/xhtml1/#C_16 */ private static final String XML_APOSTROPHE = "'"; /** * Writes out an attribute for an element. If the attribute value * is non-{@code null} and non-empty, then the attribute is written out, * preceded by a single space. * The given value will be XML encoded before appending to the buffer. * <p> * For example, given attrName="foo" and attrValue="val<bar" writes out: * <pre>foo="val&lt;bar"</pre> * * @param attrName the attribute name * @param attrValue the attribute value * @param buf the {@code Appendable} to which to append the attribute * name-value pair * @throws IOException from {@code Appendable} (but {@code StringBuffer} or * {@code StringBuilder} will never actually throw {@code IOException}) * @since 2.4 */ public static void xmlAppendAttr(String attrName, String attrValue, Appendable buf) throws IOException { if (attrValue != null && attrValue.length() > 0) { buf.append(' '); buf.append(attrName); buf.append("=\""); xmlAppendAttrValue(attrValue, buf); buf.append('"'); } } /** * XML encodes an attribute value, escaping some characters as * character entities, and dropping invalid control characters. * <p> * Only four characters need to be encoded, according to the * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char"> * W3C XML 1.0 Specification Character definition</a>: {@code < & " '} * (less-than, ampersand, double-quote, single-quote). * <p> * Actually, we could only encode one of the quote characters if * we knew that that was the one used to wrap the value, but we'll * play it safe and encode both. * <p> * We drop invalid XML characters, following the * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char"> * W3C XML 1.0 Specification Character definition</a>: * <pre> * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] * </pre> * Java uses UTF-16 internally, so Unicode characters U+10000 to * U+10FFFF are encoded using the surrogate characters excluded * above, 0xD800 to 0xDFFF. So we allow just 0x09, 0x0A, 0x0D, * and the range 0x20 to 0xFFFD. * * @param attrValue the attribute value * @param buf the {@code Appendable} to which to append the attribute value * @throws IOException from {@code Appendable} (but {@code StringBuffer} or * {@code StringBuilder} will never actually throw {@code IOException}) * @since 2.4 */ public static void xmlAppendAttrValue(String attrValue, Appendable buf) throws IOException { for (int i = 0; i < attrValue.length(); i++) { char c = attrValue.charAt(i); switch (c) { case '<': buf.append(XML_LESS_THAN); break; case '&': buf.append(XML_AMPERSAND); break; case '"': buf.append(XML_QUOTE); break; case '\'': buf.append(XML_APOSTROPHE); break; case '\t': case '\n': case '\r': // TODO: what happens to white-space? buf.append(c); break; default: if (c >= 0x20 && c <= 0xFFFD) { buf.append(c); } break; } } } }