import java.util.Stack;
import java.util.Vector;
public class StringXmlWriter {
private StringBuilder m_xml;//输出端
private int m_iLevel;//用来处理缩进
private Vector<String> m_attrVect;//存放属性,只存放当前标签的属性, 输出标签后清除
private Stack<String> m_tagStack;//存放标签, 用来自动输出关闭标签
public static void main(String[] args) {
//Example
StringXmlWriter billXml = new StringXmlWriter();
billXml.addComment("示例");
billXml.addAttributes("attr", "ROOT");//在添加标签前, 你可以添加任意个属性
billXml.createTag("Bills");//添加标签
billXml.addAttributes("attr", "单据");
billXml.createTag("Bill");
billXml.addComment("表头");
billXml.createTextTag("pk_corp", "1001");
billXml.createTextTag("pk_billtype", "YXB8");
billXml.addComment("表体");
billXml.createTag("Bodys");
billXml.addAttributes("attr", "第一行");
billXml.createTag("Body");
billXml.createTextTag("pk_corp", "1001");
billXml.closeLastTag();
billXml.addAttributes("attr", "第二行");
billXml.createTag("Body");
billXml.createTextTag("pk_corp", "1002");
billXml.closeLastTag();
billXml.closeAllTags();
System.out.println(billXml);
}
public StringXmlWriter()
{
super();
m_xml = new StringBuilder();
m_attrVect = new Vector<String>();
m_tagStack = new Stack<String>();
String encoding = null;
try{
encoding = "utf-8";
}catch (Exception e) {
}
m_xml.append("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
}
/**
* 添加xml标签, 如果标签有属性, 则必须先添加属性
* @param tag
* @author wuzq
* @date 2014-11-6 下午3:33:48
*/
public void createTag(String tag){
m_xml.append("\n");
for(int i =0;i<m_iLevel;i++)
m_xml.append("\t");
m_xml.append("<").append(tag);
addAttrToTag();
m_xml.append(">");
m_tagStack.push(tag);
m_iLevel++;
}
private void addAttrToTag() {
while(0 < m_attrVect.size()/2){
String attr = m_attrVect.remove(m_attrVect.size()-1);
m_xml.append(" ").append(attr).append("=");
attr = m_attrVect.remove(m_attrVect.size()-1);
m_xml.append("\"").append(attr).append("\"");
}
m_attrVect.clear();
}
/**
* 关闭标签
*
* @author wuzq
* @date 2014-11-6 下午3:38:28
*/
public void closeLastTag(){
m_xml.append("\n");
m_iLevel--;
for(int i=0; i<m_iLevel; i++){
m_xml.append("\t");
}
m_xml.append("</").append(m_tagStack.pop()).append(">");
}
/**
* 关闭所有标签
*
* @author wuzq
* @date 2014-11-6 下午3:49:44
*/
public void closeAllTags(){
while(m_tagStack.size() != 0){
m_xml.append("\n");
m_iLevel--;
for(int i=0;i<m_iLevel; i++){
m_xml.append("\t");
}
m_xml.append("</").append(m_tagStack.pop()).append(">");
}
}
/**
* 添加子标签, 如果子标签有属性, 则必须先添加属性
* @param tag
* @param value
* @author wuzq
* @date 2014-11-6 下午3:50:37
*/
public void createTextTag(String tag, String value){
m_xml.append("\n");
for(int i=0; i<m_iLevel; i++){
m_xml.append("\t");
}
m_xml.append("<").append(tag);
addAttrToTag();
m_xml.append(">").append(value).append("</").append(tag).append(">");
}
/**
* 添加属性
* @param key
* @param value
* @author wuzq
* @date 2014-11-6 下午3:56:04
*/
public void addAttributes(String key, String value){
m_attrVect.addElement(value);
m_attrVect.addElement(key);
}
/**
* 添加注释
* @param comment
* @author wuzq
* @date 2014-11-6 下午3:58:48
*/
public void addComment(String comment){
m_xml.append("\n");
for(int i=0; i<m_iLevel; i++){
m_xml.append("\t");
}
m_xml.append("<!--").append(comment).append("-->");
}
@Override
public String toString() {
return m_xml.toString();
}
}
输出:
<?xml version="1.0" encoding="utf-8"?>
<!--示例-->
<Bills attr="ROOT">
<Bill attr="单据">
<!--表头-->
<pk_corp>1001</pk_corp>
<pk_billtype>YXB8</pk_billtype>
<!--表体-->
<Bodys>
<Body attr="第一行">
<pk_corp>1001</pk_corp>
</Body>
<Body attr="第二行">
<pk_corp>1002</pk_corp>
</Body>
</Bodys>
</Bill>
</Bills>