Appends an xml attribute into the StringBuilder - Java XML

Java examples for XML:XML Attribute

Description

Appends an xml attribute into the StringBuilder

Demo Code


import java.util.Map;

public class Main{
    public static void main(String[] argv) throws Exception{
        StringBuilder sb = new StringBuilder();
        String att = "java2s.com";
        String value = "java2s.com";
        att(sb,att,value);//  www . j  a v  a  2s . c  o m
    }
    /** 
     * Appends an xml attribute into the StringBuilder
     * @param sb Mutable String
     * @param att Name of the attribute
     * @param value Non null value to fill into the string
     */
    private static void att(StringBuilder sb, String att, String value) {
        sb.append(" ");
        sb.append(att);
        sb.append("=\"");
        sb.append(value.toString());
        sb.append("\"");
    }
    /** 
     * Appends an xml attribute into the StringBuilder
     * @param sb Mutable String
     * @param att Name of the attribute
     * @param value Value to fill into the string
     */
    private static void att(StringBuilder sb, String att, Double value) {
        if (value != null) {
            att(sb, att, NumberFormatHelper.format(value));
        }
    }
    /** 
     * Appends an xml attribute into the StringBuilder
     * @param sb Mutable String
     * @param att Name of the attribute
     * @param value Value to fill into the string
     */
    private static void att(StringBuilder sb, String att, Integer value) {
        if (value != null) {
            att(sb, att, Integer.toString(value));
        }
    }
    /** 
     * Appends an xml attribute into the StringBuilder
     * @param sb Mutable String
     * @param att Name of the attribute
     * @param value Value to fill into the string
     */
    private static void att(StringBuilder sb, String att, Long value) {
        if (value != null) {
            att(sb, att, Long.toString(value));
        }
    }
    /** 
     * Appends an xml attribute into the StringBuilder
     * @param sb Mutable String
     * @param att Name of the attribute
     * @param value Value to fill into the string.  1 = true; 0 = false
     */
    private static void att(StringBuilder sb, String att, Boolean value) {
        if (value != null) {
            att(sb, att, value ? "1" : "0");
        }
    }
}

Related Tutorials