java使用Jdom实现xml文件写入操作实例
作者:5iasp 时间:2022-09-30 02:38:23
本文实例讲述了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程序设计有所帮助。
标签:java,Jdom,xml
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java详细讲解不同版本的接口语法和抽象类与接口的区别
2022-09-30 01:46:38
![](https://img.aspxhome.com/file/2023/5/61875_0s.png)
Java 数组高频考点分析讲解
2021-09-01 13:14:36
![](https://img.aspxhome.com/file/2023/2/61152_0s.png)
搞懂Java线程池
2021-08-04 10:01:06
![](https://img.aspxhome.com/file/2023/5/62345_0s.jpg)
使用C#发送Http请求实现模拟登陆实例
2023-06-22 22:25:07
![](https://img.aspxhome.com/file/2023/7/67047_0s.png)
mybatis使用collection嵌套查询的实现
2021-07-06 22:04:58
![](https://img.aspxhome.com/file/2023/1/71861_0s.png)
Idea热加载插件JRebel激活以及使用教程
2022-03-27 18:16:34
![](https://img.aspxhome.com/file/2023/5/66185_0s.jpg)
Spring+SpringMVC+MyBatis深入学习及搭建(二)之MyBatis原始Dao开发和mapper代理开发
2021-07-24 06:36:00
![](https://img.aspxhome.com/file/2023/4/67124_0s.png)
使用JDBC实现数据访问对象层(DAO)代码示例
2021-11-12 23:33:46
Flyway详解及Springboot集成Flyway的详细教程
2022-09-21 10:37:49
![](https://img.aspxhome.com/file/2023/4/70304_0s.jpg)
Java泛型T,E,K,V,N,?与Object区别和含义
2022-02-23 07:25:30
![](https://img.aspxhome.com/file/2023/7/64747_0s.jpg)
Spring整合mybatis实现过程详解
2022-05-23 02:10:23
![](https://img.aspxhome.com/file/2023/5/71605_0s.png)
SpringBoot下如何实现支付宝接口的使用
2023-11-06 14:26:15
![](https://img.aspxhome.com/file/2023/7/58747_0s.png)
解析spring事务管理@Transactional为什么要添加rollbackFor=Exception.class
2021-09-03 17:07:41
![](https://img.aspxhome.com/file/2023/4/72314_0s.png)
Java JDK动态代理(AOP)用法及实现原理详解
2021-11-14 16:45:21
![](https://img.aspxhome.com/file/2023/2/69292_0s.png)
通过JDK源码角度分析Long类详解
2022-03-11 19:26:38
Java最简洁数据结构之冒泡排序快速理解
2023-12-01 06:31:52
![](https://img.aspxhome.com/file/2023/8/60278_0s.png)
Mybatis中的延迟加载案例解析
2023-02-27 01:55:37
![](https://img.aspxhome.com/file/2023/1/68221_0s.png)
Java sha1散列算法原理及代码实例
2021-06-28 05:50:22
浅谈C#与Java两种语言的比较
2023-09-26 13:05:19
java方法及this关键字原理分析详解
2021-08-16 07:51:35
![](https://img.aspxhome.com/file/2023/4/73054_0s.png)