Java 怎么使用指定的属性创建一个空的和关闭的XML节点

2019年7月21日19:47:03Java 怎么使用指定的属性创建一个空的和关闭的XML节点已关闭评论 513 2106字阅读7分1秒

1.简介

Java 怎么使用指定的属性创建一个空的和关闭的XML节点?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import java.util.Map;

public class Main{
    public static void main(String[] argv) throws Exception{
        String elementName = "tah1986.com";
        System.out.println(element(elementName));
    }
    
    public static String element(String elementName) {
        return element(elementName, null, null, true);
    }
    
    public static String element(String elementName, Table<Object> closure) {
        return element(elementName, closure, null, true);
    }
    
    public static String element(String elementName, Table<Object> closure,
            boolean close) {
        return element(elementName, closure, null, close);
    }
    
    public static String element(String elementName, Table<Object> closure,
            String content) {
        return element(elementName, closure, content, true);
    }
    
    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();
    }
    
    private static void att(StringBuilder sb, String att, String value) {
        sb.append(" ");
        sb.append(att);
        sb.append("=\"");
        sb.append(value.toString());
        sb.append("\"");
    }
    
    private static void att(StringBuilder sb, String att, Double value) {
        if (value != null) {
            att(sb, att, NumberFormatHelper.format(value));
        }
    }
    
    private static void att(StringBuilder sb, String att, Integer value) {
        if (value != null) {
            att(sb, att, Integer.toString(value));
        }
    }
    
    private static void att(StringBuilder sb, String att, Long value) {
        if (value != null) {
            att(sb, att, Long.toString(value));
        }
    }
    
    private static void att(StringBuilder sb, String att, Boolean value) {
        if (value != null) {
            att(sb, att, value ? "1" : "0");
        }
    }
}

 
继续阅读
编程十万个怎么办