xml的添加

package xml2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.ProcessingInstruction;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.sun.org.apache.bcel.internal.generic.NEW;


public class test {

    private static int times=0;
    
    public static void main(String [] args) {
        
        Format format=Format.getCompactFormat();
         format.setEncoding("gb2312");
         format.setIndent("    ");
           XMLOutputter XMLOut=new XMLOutputter(format);
        
        
        try {
              
            if (times!=0) {
                
                Element root,task,id,Task,Status,Solver,Priority,Time,Node;
                   root=new Element("TaskList");
                   task=new Element("task");
                   id=new Element("id");
                   Task=new Element("Task");
                   Status=new Element("Status");
                   Solver=new Element("Solver");
                   Priority=new Element("Priority");
                   Time=new Element("Time");
                   Node=new Element("Node");
                   Document doc=new Document(root);
                  // id.setText("0");
                   id.setText(String.valueOf(0));
                  // Task.setText("sphere.cst");
                  Task.setText("cst");
                   Status.setText("OK");
                   Solver.setText("CST");
                   Priority.setText("1");
                   //Time.setText("2013年10月12日");
                   Time.setText("11.18");
                   Node.setText("172.18.14.9");
                   task.addContent(id);
                   task.addContent(Task);
                   task.addContent(Status);
                   task.addContent(Solver);
                   task.addContent(Priority);
                   task.addContent(Time);
                   task.addContent(Node);
                   root.addContent(task);
                  
                  
                   FileOutputStream fileOutputStream=new FileOutputStream("D:\\config.xml");
                   
                   XMLOut.output(doc,fileOutputStream);
                   
                   times++;
                   
            }else {
                
                  SAXBuilder builder = new SAXBuilder();

                  Document doc = builder.build("D:\\config.xml");//获得文档对象
                  
                  Element root = doc.getRootElement();//获得根节点
                  
                  Element task,id,Task,Status,Solver,Priority,Time,Node;
                  
                  task = new Element("task");
                   
                  //element.setAttribute("id", "3");
                   
                   id = new Element("id");
                   id.setText("1");
                   Task = new Element("Task");
                   Task.setText("sphere.cst");
                   Status=new Element("Status");
                   Status.setText("OK");
                   Solver=new Element("Solver");
                   Solver.setText("CST");
                   Priority=new Element("Priority");
                   Priority.setText("1");
                   Time=new Element("Time");
                   Time.setText("11.8");
                   Node=new Element("Node");
                   Node.setText("172.18.14.9");
                   task.addContent(id);
                   task.addContent(Task);
                   task.addContent(Status);
                   task.addContent(Solver);
                   task.addContent(Priority);
                   task.addContent(Time);
                   task.addContent(Node);
                   root.addContent(task);
                   doc.setRootElement(root);
                  
                   //文件处理
                  // XMLOutputter out = new XMLOutputter();
                   //out.output(doc, new FileOutputStream("D:\\config.xml"));
                   XMLOut.output(doc,new FileOutputStream("D:\\config.xml"));
                  times++;

                
            }
          
         
           
           }
           catch(Exception e)
           {
              System.out.println(e.toString());
           }/**/
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值