xml的添加
来源:互联网 发布:淘宝上的缩阴棒放心吗 编辑:程序博客网 时间:2024/06/11 18:42
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());
}/**/
}
}
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());
}/**/
}
}
- xml的添加
- 添加XML记录的方法
- C# 添加xml的节点
- Struts中常要添加的XML
- xml文件的建立、添加、查询
- xml文件的建立、添加、查询
- XML的增加项目.添加属性 等
- 给XML文件添加新的节点
- Eclipse 添加Spring XML配置文件的提示
- Eclipse 添加Spring XML配置文件的提示
- web.xml里面添加注释的方法<!-- -->
- Android功能添加Ant的build.xml
- Eclipse 添加Spring XML配置文件的提示
- 使用XML实现悬浮的添加按钮
- pom.xml 添加 JSON依赖的配置
- eclipse中添加xml的智能提示
- eclipse中添加xml的智能提示
- android 添加组件的xml 文件
- C#中enum的总结
- Linux内核kobject模型(添加kobject对象,模拟kobject状态变化)
- cocos2d-x-LuaProxy学习日志(6) -- C/S通信交互之WebSocket
- 几个有意思的算法题(高斯日记、排它平方数、振兴中华、颠倒的价牌)
- 在spring中通过hibernate配置C3P0
- xml的添加
- shope实例
- Spring MVC 框架搭建及详解
- python学习:函数参数的packing和unpacking传递
- jquery选择器中基本选择器
- Android产品设计规范
- “黑暗面程序员”IO流FileWrite例子
- Accelerated C++ Exercise 5-9
- Prewitt算子