java使用Jdom实现xml文件写入操作实例
作者:5iasp 发布时间:2022-09-30 02:38:23
标签:java,Jdom,xml
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- mybatis-spring:@MapperScan注解在demo: springboot+mybatis的示例中,dao层接口使用了注解@
- 在 Java 语言中的类初始化块 文章中我们简单的介绍了下 Java 中的实例初始化块 ( IIB )。不过我觉得介绍的有点简单了,于是,再
- 写出一个struct,然后sizeof,你会不会经常对结果感到奇怪?sizeof的结果往往都比你声明的变量总长度要大,这是怎么回事呢?讲讲字
- 一、前台服务的简单介绍前台服务是那些被认为用户知道且在系统内存不足的时候不允许系统杀死的服务。前台服务必须给状态栏提供一个通知,它被放到正在
- 一、读取系统配置文件application.yaml1、application.yaml配置文件中增加一下测试配置testdata: &nb
- 在Web的应用方面有js的插件实现自动完成(或叫智能提示)功能,但在WinForm窗体应用方面就没那么好了。TextBox控件本身是提供了一
- 本文为大家分享了Toast类避免显示时间叠加的方法,供大家参考,具体内容如下import android.app.Activity;impo
- Chart控件可以用来绘制波形图、柱状图、饼图、折线图等,用来进行数据表现是很不错的简单说一下这个控件的使用方法效果图我们首先要加载Char
- 照片墙这种功能现在应该算是挺常见了,在很多应用中你都可以经常看到照片墙的身影。它的设计思路其实也非常简单,用一个GridView控件当作“墙
- 在开发过程中,我们可能会经常遇到这样的需求样式:这张图是截取京东消息通知的弹出框,我们可以看到右上方有个三角形的气泡效果,这只是其中一种,三
- 概述日常工作中,我们经常会有发送 HTTP 网络请求的需求,概括下我们常见的发送 HTTP 请求的需求内容:可以发送基本的 GET/POST
- — 遇到问题今天在IDEA里面运行项目的时候报了一个错,如下图所示:— 找到问题根源其实控制台给出的错误信息提示说的很明显:类加载器加载文件
- 本文实例总结了C#中split用法。分享给大家供大家参考,具体如下:以下是我转载的两个不同的人的,方便大家及自己查阅string s=&qu
- 1. 算法分析根据概率将奖品划分区间,每个区间代表一个奖品,然后抽取 随机数,反查落在那个区间上,即为所抽取的奖品。2. 代码核心
- 前言想必大家对c语言的动态内存分配并不陌生,忘了的小伙伴也可以看看我的这篇文章C语言动态内存分配c语言的动态内存分配由于有些地方用起来比较麻
- 本文实例讲述了Android实现Activity、Service与Broadcaster三大组件之间互相调用的方法。分享给大家供大家参考,具
- ✨字符, 字节与字符串🎈字符与字符串字符串内部包含一个字符数组,String 可以和 char[] 相互转换.NO方法名称类型描述1publ
- 在Android 5.0以后的版本中,定义一个button时,系统自动会加一个阴影的效果,有的时候这种效果看起来比较好,有的时候不符合UI的
- 安装 Tomcat 之前请一定先安装 Java ,然后才能安装 Tomcat 。安装 Java 、环境变量 path 的设置以及 cmd 小
- 背景前些天遇到一个需求,在没有第三方源码的情况下,刷新一个第三方UI,并且拦截到其ajax请求的返回结果。当结果为AVALIABLE的时候,