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

因为要用到拼接一些值成xml字符串所以写下了一个下工具类:


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;
}
}
}




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();
}
}





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));
}
}

运行结果:
[color=blue]<node><node1 name="zhangshan" password="123465" >11111</node1><node1 name="lishi" password="3333" >11111</node1></node>[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值