java 生成XML格式的字符串,支持多种XML格式,具体可以看类内方法:

代码:

 

 
  
  1. import java.util.ArrayList;  
  2. import java.util.Iterator;  
  3. import java.util.List;  
  4.  
  5. /**  
  6.  * 根据该对象可以构造Xml字符串  
  7.  *  
  8.  *  
  9.  */  
  10. public class XmlObject {  
  11.     private static String HEAD = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";  
  12.     private String name;  
  13.     private Object value;  
  14.     private List<Attribute> attributes;  
  15.     private List<XmlObject> subXmlObjects;  
  16.  
  17.     /**  
  18.      * 根据name构造XmlObject  
  19.      * @param name 生成xml时标签名,如name="html",则生成xml为<html/> 
  20.      */  
  21.     public XmlObject(String name) {  
  22.         this.name = name;  
  23.     }  
  24.  
  25.     /**  
  26.      * 获得当前对象的名称  
  27.      * @return 返回当前对象的名称  
  28.      */  
  29.     public final String getName() {  
  30.         return name;  
  31.     }  
  32.  
  33.     /**  
  34.      * 设置当前对象的名称  
  35.      * @param name 给定名称  
  36.      */  
  37.     public final void setName(String name) {  
  38.         this.name = name;  
  39.     }  
  40.  
  41.     /**  
  42.      * 获得当前对象的值  
  43.      * @return 返回当前对象的值  
  44.      */  
  45.     public final Object getValue() {  
  46.         return value;  
  47.     }  
  48.  
  49.     /**  
  50.      * 设置当前对象的值  
  51.      * @param value 给定值  
  52.      */  
  53.     public final void setValue(Object value) {  
  54.         this.value = value;  
  55.     }  
  56.  
  57.     /**  
  58.      * 为当前XmlObject添加属性  
  59.      * @param name 属性名  
  60.      * @param value 属性值  
  61.      */  
  62.     public final void setAttribute(String name, Object value) {  
  63.         if (attributes == null) {  
  64.             attributes = new ArrayList<Attribute>();  
  65.         }  
  66.         Attribute attribute = null;  
  67.         for (Attribute att : attributes) {  
  68.             if (name.equalsIgnoreCase(att.getName())) {  
  69.                 attattribute = att;  
  70.                 break;  
  71.             }  
  72.         }  
  73.         if (attribute == null) {  
  74.             attribute = new Attribute(name, value);  
  75.             attributes.add(attribute);  
  76.         } else {  
  77.             attribute.setValue(value);  
  78.         }  
  79.     }  
  80.  
  81.     /**  
  82.      * 根据属性名称获得当前XmlObject对象的属性值  
  83.      * @param name 属性名称  
  84.      * @return 属性值  
  85.      */  
  86.     public final Object getAttributeValue(String name) {  
  87.         return getAttributeValue(name, null);  
  88.     }  
  89.  
  90.     /**  
  91.      * 根据属性名称获得当前XmlObject对象的属性值  
  92.      * @param name 属性名称  
  93.      * @param defaultValue 默认值  
  94.      * @return 若属性存在,则返回属性值,否则返回默认值  
  95.      */  
  96.     public final Object getAttributeValue(String name, Object defaultValue) {  
  97.         Attribute attribute = null;  
  98.         for (Attribute att : attributes) {  
  99.             if (name.equalsIgnoreCase(att.getName())) {  
  100.                 attattribute = att;  
  101.                 break;  
  102.             }  
  103.         }  
  104.         if (attribute == null) {  
  105.             return defaultValue;  
  106.         } else {  
  107.             return attribute.getValue();  
  108.         }  
  109.     }  
  110.  
  111.     /**  
  112.      * 为当前XmlObject对象添加子XmlObject对象  
  113.      * @param xmlObject 给定XmlObject对象  
  114.      */  
  115.     public final void addSubXmlObject(XmlObject xmlObject) {  
  116.         if (subXmlObjects == null) {  
  117.             subXmlObjects = new ArrayList<XmlObject>();  
  118.         }  
  119.         subXmlObjects.add(xmlObject);  
  120.     }  
  121.  
  122.     /**  
  123.      * 构造当前对象的压缩XML字符串  
  124.      *   
  125.      * @return XML字符串  
  126.      */  
  127.     public final String toCompactXml() {  
  128.         return HEAD + getNoHeadXml("", "");  
  129.     }  
  130.  
  131.     /**  
  132.      * 根据格式化留白("\t")和默认的行分隔符("\t")构造当前对象的XML字符串  
  133.      *   
  134.      * @return XML字符串  
  135.      */  
  136.     public String toFormatXml() {  
  137.         return toFormatXml("\t");  
  138.     }  
  139.  
  140.     /**  
  141.      * 根据格式化留白和默认的行分隔符构("\n")造当前对象的XML字符串  
  142.      *   
  143.      * @param blank  
  144.      *            格式化留白  
  145.      * @return XML字符串  
  146.      */  
  147.     protected final String toFormatXml(String blank) {  
  148.         return HEAD + "\n" + getNoHeadXml(blank, "\n");  
  149.     }  
  150.  
  151.     /**  
  152.      * 根据格式化留白和行分隔符构造当前对象的无头的XML字符串  
  153.      *   
  154.      * @param blank  
  155.      *            格式化留白  
  156.      * @param split  
  157.      *            行分隔符  
  158.      * @return 无头的XML字符串  
  159.      */  
  160.     protected final String getNoHeadXml(String blank, String split) {  
  161.         return getNoHeadXml(blank, split, 0);  
  162.     }  
  163.  
  164.     private final String getNoHeadXml(String blank, String split, int count) {  
  165.         String blanks = "";  
  166.         for (int i = 0; i < count; i++) {  
  167.             blanks += blank;  
  168.         }  
  169.         StringBuffer sb = new StringBuffer();  
  170.         sb.append(blanks + "<" + name);  
  171.         if (attributes != null) {  
  172.             Iterator<Attribute> iterator = attributes.iterator();  
  173.             while (iterator.hasNext()) {  
  174.                 Attribute attribute = iterator.next();  
  175.                 sb.append(" " + attribute.getName() + "=\""  
  176.                         + attribute.getValue() + "\"");  
  177.             }  
  178.         }  
  179.         if (subXmlObjects == null) {  
  180.             if (value == null) {  
  181.                 sb.append("/>" + split);  
  182.             } else {  
  183.                 sb.append(">");  
  184.                 sb.append(value);  
  185.                 sb.append("</" + name + ">" + split);  
  186.             }  
  187.         } else {  
  188.             sb.append(">" + split);  
  189.             Iterator<XmlObject> iterator = subXmlObjects.iterator();  
  190.             count += 1;  
  191.             while (iterator.hasNext()) {  
  192.                 XmlObject xmlObject = iterator.next();  
  193.                 sb.append(xmlObject.getNoHeadXml(blank, split, count));  
  194.             }  
  195.             sb.append(blanks + "</" + name + ">" + split);  
  196.         }  
  197.         return sb.toString();  
  198.     }  
  199.  
  200.     class Attribute {  
  201.         private String name;  
  202.         private Object value;  
  203.  
  204.         public Attribute(String name, Object value) {  
  205.             this.name = name;  
  206.             this.value = value;  
  207.         }  
  208.  
  209.         public String getName() {  
  210.             return name;  
  211.         }  
  212.  
  213.         public void setName(String name) {  
  214.             this.name = name;  
  215.         }  
  216.  
  217.         public Object getValue() {  
  218.             return value;  
  219.         }  
  220.  
  221.         public void setValue(Object value) {  
  222.             this.value = value;  
  223.         }  
  224.     }