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

 

目录
相关文章
|
7月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
180 2
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
XML 前端开发 Java
讲解SSM的xml文件
本文详细介绍了SSM框架中的xml配置文件,包括springMVC.xml和applicationContext.xml,涉及组件扫描、数据源配置、事务管理、MyBatis集成以及Spring MVC的视图解析器配置。
275 1
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
Java Maven
maven项目的pom.xml文件常用标签使用介绍
第四届人文,智慧教育与服务管理国际学术会议(HWESM 2025) 2025 4th International Conference on Humanities, Wisdom Education and Service Management
969 8
|
XML Android开发 数据格式
Eclipse 创建 XML 文件
Eclipse 创建 XML 文件
191 2
|
XML JavaScript Java
java与XML文件的读写
java与XML文件的读写
161 3
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
252 1
|
XML 存储 缓存
C#使用XML文件的详解及示例
C#使用XML文件的详解及示例
559 0
|
XML 存储 Web App开发
查看 XML 文件
查看 XML 文件
|
XML Java 数据格式
支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法
支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法