Java examples for XML:XML Attribute
Creates an empty and closed xml node with the indicated attributes
import java.util.Map; public class Main{ public static void main(String[] argv) throws Exception{ String elementName = "java2s.com"; System.out.println(element(elementName)); }//from ww w . j a va2s . co m /** * Creates an empty and closed xml node with the indicated attributes * @param elementName Name of the element * @return String representation of the XML node */ public static String element(String elementName) { return element(elementName, null, null, true); } /** * Creates an empty and closed xml node with the indicated attributes * @param elementName Name of the element * @param closure Table with the attributes * @return String representation of the XML node */ public static String element(String elementName, Table<Object> closure) { return element(elementName, closure, null, true); } /** * Creates an empty xml node with the indicated attributes * @param elementName Name of the element * @param closure Table with the attributes * @param close If it is true, the method closes the xml node; if false, it * does not * @return String representation of the XML node */ public static String element(String elementName, Table<Object> closure, boolean close) { return element(elementName, closure, null, close); } /** * Creates a closed xml node with the indicated attributes and content * @param elementName Name of the element * @param closure Table with the attributes * @param content Content of the XML node * @return String representation of the XML node */ public static String element(String elementName, Table<Object> closure, String content) { return element(elementName, closure, content, true); } /** * Creates an xml node with the indicated attributes and content * @param elementName Name of the element * @param closure Table with the attributes * @param content Content of the XML node * @param close If it is true, the method closes the xml node; if false, it * does not * @return String representation of the XML node */ public static String element(String elementName, Table<Object> closure, String content, boolean close) { StringBuilder sb = new StringBuilder(); sb.append("<"); sb.append(elementName); if (closure != null) { Map<String, Object> map = closure.map(); if (map != null && map.size() != 0) { sb.append(" "); for (String key : map.keySet()) { Object value = map.get(key); if (value instanceof String) { att(sb, key, (String) value); } else if (value instanceof Double) { att(sb, key, (Double) value); } else if (value instanceof Boolean) { att(sb, key, (Boolean) value); } else if (value instanceof Long) { att(sb, key, (Long) value); } else if (value instanceof Integer) { att(sb, key, (Integer) value); } else { att(sb, key, value.toString()); } } } } if (content != null) { sb.append(">"); sb.append(content); if (close) { sb.append("</"); sb.append(elementName); sb.append(">"); } } else { sb.append(close ? "/>" : ">"); } return sb.toString(); } /** * 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"); } } }