【原创】组装xml字符串工具类

因为要用到拼接一些值成xml字符串所以写下了一个下工具类:
Java代码 复制代码  收藏代码
  1.   
  2. import java.util.ArrayList;   
  3. import java.util.HashMap;   
  4. import java.util.List;   
  5. import java.util.Map;   
  6.   
  7. /**  
  8.  * XML元素类  
  9.  
  10.  * @author tangyangbo  
  11.  */  
  12. public class Element {   
  13.     private String name;                 //元素名   
  14.     private String nodeText = "";        //文本值   
  15.     private Map<String,String> property = new HashMap<String,String>(); //属性   
  16.     private boolean isleaf = true;       //是否子节点   
  17.     private List<Element> child = new ArrayList<Element>();         //子节点   
  18.        
  19.     public Element(String name) {   
  20.         this.name = name;   
  21.     }   
  22.        
  23.     public String getName() {   
  24.         return name;   
  25.     }   
  26.   
  27.     public void setName(String name) {   
  28.         this.name = name;   
  29.     }   
  30.     public String getNodeText() {   
  31.         return nodeText;   
  32.     }   
  33.     public void setNodeText(String nodeText) {   
  34.         this.nodeText = nodeText;   
  35.     }   
  36.     public Map<String, String> getProperty() {   
  37.         return property;   
  38.     }   
  39.     public void setProperty(Map<String, String> property) {   
  40.         this.property = property;   
  41.     }   
  42.     public boolean isIsleaf() {   
  43.         return isleaf;   
  44.     }   
  45.     //这个方法应该屏蔽   
  46.     public void setIsleaf(boolean isleaf) {   
  47.         this.isleaf = isleaf;   
  48.     }   
  49.     public List<Element> getChild() {   
  50.         return child;   
  51.     }   
  52.     public void setChild(List<Element> child) {   
  53.         this.child = child;   
  54.         if(this.isleaf && this.child.size() > 0){   
  55.             this.isleaf = false;   
  56.         }   
  57.     }   
  58.   
  59.     /**  
  60.      * 添加属性  
  61.      * @param key  
  62.      * @param value  
  63.      */  
  64.     public void addProperty(String key,String value){   
  65.         this.property.put(key, value);   
  66.     }   
  67.        
  68.     /**  
  69.      * 添加子节点  
  70.      * @param el  
  71.      */  
  72.     public void addChild(Element el){   
  73.         this.child.add(el);   
  74.         if(this.isleaf && this.child.size() > 0){   
  75.             this.isleaf = false;   
  76.         }   
  77.     }   
  78. }  
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * XML元素类

 * @author tangyangbo
 */
public class Element {
	private String name;                 //元素名
	private String nodeText = "";        //文本值
	private Map<String,String> property = new HashMap<String,String>(); //属性
	private boolean isleaf = true;       //是否子节点
	private List<Element> child = new ArrayList<Element>();         //子节点
	
	public Element(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public String getNodeText() {
		return nodeText;
	}
	public void setNodeText(String nodeText) {
		this.nodeText = nodeText;
	}
	public Map<String, String> getProperty() {
		return property;
	}
	public void setProperty(Map<String, String> property) {
		this.property = property;
	}
	public boolean isIsleaf() {
		return isleaf;
	}
	//这个方法应该屏蔽
	public void setIsleaf(boolean isleaf) {
		this.isleaf = isleaf;
	}
	public List<Element> getChild() {
		return child;
	}
	public void setChild(List<Element> child) {
		this.child = child;
		if(this.isleaf && this.child.size() > 0){
			this.isleaf = false;
		}
	}

	/**
	 * 添加属性
	 * @param key
	 * @param value
	 */
	public void addProperty(String key,String value){
		this.property.put(key, value);
	}
	
	/**
	 * 添加子节点
	 * @param el
	 */
	public void addChild(Element el){
		this.child.add(el);
		if(this.isleaf && this.child.size() > 0){
			this.isleaf = false;
		}
	}
}


Java代码 复制代码  收藏代码
  1.   
  2. import java.util.Iterator;   
  3.   
  4. /**  
  5.  * XMl工具类  
  6.  
  7.  * @author tangyangbo  
  8.  */  
  9. public class XmlUtil {   
  10.        
  11.     static String lt = "<";   
  12.     static String ltEnd = "</";   
  13.     static String rt = ">";   
  14.     static String rhtEnd = "/>";   
  15.     static String quotes = "\"";   
  16.     static String equal = "=";   
  17.     static String blank = " ";   
  18.        
  19.     public static String elementToXml(Element el){   
  20.         StringBuffer result = new StringBuffer();   
  21.         //元素开始   
  22.         result.append(lt).append(el.getName());   
  23.         //判断是否有属性   
  24.         if(el.getProperty() != null && el.getProperty().size() > 0 ){   
  25.             Iterator iter = el.getProperty().keySet().iterator();   
  26.             while (iter.hasNext()) {   
  27.                 String key = String.valueOf(iter.next());   
  28.                 String value = el.getProperty().get(key);   
  29.                 result.append(blank).append(key).append(equal)   
  30.                 .append(quotes).append(value).append(quotes).append(blank);   
  31.             }   
  32.         }   
  33.         result.append(rt);//结束标记   
  34.         /*  
  35.          * 判断是否是叶子节点  
  36.          * 是叶子节点,添加节点内容  
  37.          * 不是叶子节点,循环添加子节点  
  38.          */  
  39.         if(el.isIsleaf()){   
  40.             result.append(el.getNodeText());   
  41.         }else{   
  42.             for(Element element :el.getChild()){   
  43.                 result.append(elementToXml(element));   
  44.             }   
  45.         }   
  46.         //元素结束   
  47.         result.append(ltEnd).append(el.getName()).append(rt);   
  48.         return result.toString();   
  49.     }   
  50. }  
import java.util.Iterator;

/**
 * XMl工具类

 * @author tangyangbo
 */
public class XmlUtil {
	
	static String lt = "<";
	static String ltEnd = "</";
	static String rt = ">";
	static String rhtEnd = "/>";
	static String quotes = "\"";
	static String equal = "=";
	static String blank = " ";
	
	public static String elementToXml(Element el){
		StringBuffer result = new StringBuffer();
		//元素开始
		result.append(lt).append(el.getName());
		//判断是否有属性
		if(el.getProperty() != null && el.getProperty().size() > 0 ){
			Iterator iter = el.getProperty().keySet().iterator();
			while (iter.hasNext()) {
				String key = String.valueOf(iter.next());
				String value = el.getProperty().get(key);
				result.append(blank).append(key).append(equal)
				.append(quotes).append(value).append(quotes).append(blank);
			}
		}
		result.append(rt);//结束标记
		/*
		 * 判断是否是叶子节点
		 * 是叶子节点,添加节点内容
		 * 不是叶子节点,循环添加子节点
		 */
		if(el.isIsleaf()){
			result.append(el.getNodeText());
		}else{
			for(Element element :el.getChild()){
				result.append(elementToXml(element));
			}
		}
		//元素结束
		result.append(ltEnd).append(el.getName()).append(rt);
		return result.toString();
	}
}



Java代码 复制代码  收藏代码
  1.   
  2. import java.util.Iterator;   
  3.   
  4. /**  
  5.  * xml测试类  
  6.  
  7.  * @author tangyangbo  
  8.  */  
  9. public class XMLTest {   
  10.   
  11.     /**  
  12.      * @param args  
  13.      */  
  14.     public static void main(String[] args) {   
  15.         Element el = new Element("node");   
  16.            
  17.         Element el1 = new Element("node1");   
  18.         el1.addProperty("name""zhangshan");   
  19.         el1.addProperty("password""123465");   
  20.         el1.setNodeText("11111");   
  21.         el.addChild(el1);   
  22.            
  23.         Element el2 = new Element("node1");   
  24.         el2.addProperty("name""lisi");   
  25.         el2.addProperty("password""3333");   
  26.         el2.setNodeText("11111");   
  27.         el.addChild(el2);   
  28.         System.out.println(XmlUtil.elementToXml(el));   
  29.     }   
  30. }  
import java.util.Iterator;

/**
 * xml测试类

 * @author tangyangbo
 */
public class XMLTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Element el = new Element("node");
		
		Element el1 = new Element("node1");
		el1.addProperty("name", "zhangshan");
		el1.addProperty("password", "123465");
		el1.setNodeText("11111");
		el.addChild(el1);
		
		Element el2 = new Element("node1");
		el2.addProperty("name", "lisi");
		el2.addProperty("password", "3333");
		el2.setNodeText("11111");
		el.addChild(el2);
		System.out.println(XmlUtil.elementToXml(el));
	}
}

运行结果:
<node><node1 name="zhangshan"  password="123465" >11111</node1><node1 name="lishi"  password="3333" >11111</node1></node>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值