Adds an XML attribute to the given StringBuffer . - Java XML

Java examples for XML:XML Attribute

Description

Adds an XML attribute to the given StringBuffer .

Demo Code

/*******************************************************************************
 * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany
 *                    Technical University Darmstadt, Germany
 *                    Chalmers University of Technology, Sweden
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  ww . j a  v  a  2s .c  om*/
 *    Technical University Darmstadt - initial API and implementation and/or initial documentation
 *******************************************************************************/
import java.util.Map;
import java.util.Map.Entry;

public class Main{
    public static void main(String[] argv) throws Exception{
        String attributeName = "java2s.com";
        String value = "java2s.com";
        StringBuffer sb = new StringBuffer();
        appendAttribute(attributeName,value,sb);
    }
    /**
     * Adds an XML attribute to the given {@link StringBuffer}.
     * @param attributeName The attribute name.
     * @param value The attribute value.
     * @param sb The {@link StringBuffer} to write to.
     */
    public static void appendAttribute(String attributeName, String value,
            StringBuffer sb) {
        if (attributeName != null && value != null) {
            sb.append(" ");
            sb.append(attributeName);
            sb.append("=\"");
            sb.append(XMLUtil.encodeText(value));
            sb.append("\"");
        }
    }
    /**
     * <p>
     * Encodes the given text in a way that it contains no XML elements
     * and can be used for instance as plain text or attribute value.
     * </p>
     * <p>
     * The following signs are replaced:
     * <pre>
     * " => &quot;quot;
     * & => &quot;amp;
     * ' => &quot;apos;
     * < => &quot;lt;
     * > => &quot;gt;
     * </pre>
     * </p>
     * @param text The text to encode.
     * @return The encoded text.
     */
    public static String encodeText(String text) {
        if (text != null) {
            char[] signs = text.toCharArray();
            StringBuffer sb = new StringBuffer();
            for (char sign : signs) {
                switch (sign) {
                case '"':
                    sb.append("&quot;");
                    break;
                case '&':
                    sb.append("&amp;");
                    break;
                case '\'':
                    sb.append("&apos;");
                    break;
                case '<':
                    sb.append("&lt;");
                    break;
                case '>':
                    sb.append("&gt;");
                    break;
                default:
                    sb.append(sign);
                    break;
                }
            }
            return sb.toString();
        } else {
            return null;
        }
    }
}

Related Tutorials