天天看点

Jdom写xml文件实例

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",

  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>