当前位置: 移动技术网 > IT编程>开发语言>Java > java使用Jdom实现xml文件写入操作实例

java使用Jdom实现xml文件写入操作实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java使用jdom实现xml文件写入操作的方法。分享给大家供大家参考,具体如下:

package com.yanek.demo.xml.test;
import java.io.file;
import java.io.filewriter;
import org.jdom.attribute;
import org.jdom.document;
import org.jdom.element;
import org.jdom.input.saxbuilder;
import org.jdom.output.xmloutputter;
public class jdomwritexml {
 /**
 * @param args
 */
 public static void main(string[] args) {
 saxbuilder sb = new saxbuilder();
 element actions = new element("actions");
 document document = new document(actions);
 element action1 = new element("action");
 actions.addcontent(action1);
 attribute path_atbt1 = new attribute("path", "/test");
 attribute class_atbt1 = new attribute("class",
  "com.mystruts.demo.loginaction");
 action1.setattribute(path_atbt1);
 action1.setattribute(class_atbt1);
 element action1_forward1 = new element("forward");
 action1.addcontent(action1_forward1);
 attribute action1_forward1_name_atbt1 = new attribute("name", "success");
 attribute action1_forward1_url_atbt1 = new attribute("url", "test.jsp");
 action1_forward1.setattribute(action1_forward1_name_atbt1);
 action1_forward1.setattribute(action1_forward1_url_atbt1);
 element action1_forward2 = new element("forward");
 action1.addcontent(action1_forward2);
 attribute action1_forward1_name_atbt2 = new attribute("name", "failure");
 attribute action1_forward1_url_atbt2 = new attribute("url",
  "failure.jsp");
 action1_forward2.setattribute(action1_forward1_name_atbt2);
 action1_forward2.setattribute(action1_forward1_url_atbt2);
 element action2 = new element("action");
 actions.addcontent(action2);
 attribute path_atbt2 = new attribute("path", "/user");
 attribute class_atbt2 = new attribute("class",
  "com.mystruts.demo.useraction");
 action2.setattribute(path_atbt2);
 action2.setattribute(class_atbt2);
 element action2_forward1 = new element("forward");
 action2.addcontent(action2_forward1);
 attribute action2_forward1_name_atbt1 = new attribute("name", "success");
 attribute action2_forward1_url_atbt1 = new attribute("url", "test.jsp");
 action2_forward1.setattribute(action2_forward1_name_atbt1);
 action2_forward1.setattribute(action2_forward1_url_atbt1);
 element action2_forward2 = new element("forward");
 action2.addcontent(action2_forward2);
 attribute action2_forward1_name_atbt2 = new attribute("name", "failure");
 attribute action2_forward1_url_atbt2 = new attribute("url",
  "failure.jsp");
 action2_forward2.setattribute(action2_forward1_name_atbt2);
 action2_forward2.setattribute(action2_forward1_url_atbt2);
 attribute root_atbt1 = new attribute("m", "001");
 actions.setattribute(root_atbt1);
 try {
  file f1 = new file("mystruts.xml");
  // xmloutputter xo=new xmloutputter(" ",true,"gb2312");
  xmloutputter xo = new xmloutputter();
  filewriter fw = new filewriter(f1);
  xo.output(document, fw);
  fw.close();
 } catch (exception e) {
  e.printstacktrace();
 }
 // system.out.println(document.tostring());
 }
}

生成xml文件:

<?xml version="1.0" encoding="utf-8"?>
<actions m="001">
<action path="/test" class="com.mystruts.demo.loginaction">
<forward name="success" url="test.jsp" />
<forward name="failure" url="failure.jsp" />
</action>
<action path="/user" class="com.mystruts.demo.useraction">
<forward name="success" url="test.jsp" />
<forward name="failure" url="failure.jsp" />
</action>
</actions>

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网