因为要用到拼接一些值成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.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 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));
- }
- }
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>