本文实例讲述了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文件:

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

java jdom_java使用Jdom实现xml文件写入操作实例相关推荐

  1. java xml中的冒号_Java jdom解析xml文件带冒号的属性

    Java jdom解析xml文件带冒号的属性 如果xml文件解析带了冒号的属性,一般都是要特别处理,这里是命名空间,N年前遇到过一次忘记记录,后来也忘了,这次再记录下. 解决了,记录下,分享给大家,百 ...

  2. java jdom 读取xml文件_使用Jdom读取XML文件方法

    使用Jdom读取XML文件方法,学习Spring时,我们经常看到很多xml配置文件,Spring通过在配置文件中的配置,使用IOC(控制反转),从而实现代码的灵活性,本篇我就为大家介绍一种解析xml方 ...

  3. Java jdom解析xml文件带冒号的属性

    Java jdom解析xml文件带冒号的属性 转载请标明出处: https://dujinyang.blog.csdn.net/article/details/99644824 本文出自:[奥特曼超人 ...

  4. java jdom 读取xml文件_jdom学习:读取xml文件

    本文转自http://www.blogjava.net/fjq639/archive/2005/12/20/24806.html 用JDOM读取XML文件需先用org.jdom.input.SAXBu ...

  5. JavaEE XML的读写(利用JDom对XML文件进行读写)

    1.有关XML的写 利用JDom2包,JDom2这个包中,至少引入org.jdom2.*;如果要进行XML文件的写出,则要进行导入org.jdom2.output.*; package com.lit ...

  6. JDom,jdom解析xml文件

    1.要解析的文件模板如下: <?xml version="1.0" encoding="GBK"?> <crsc> <data&g ...

  7. jdom解析xml文件_JDOM编辑XML文件示例

    jdom解析xml文件 JDOM provides very neat way to manipulate XML files, using JDOM is very easy and the cod ...

  8. JavaXml教程(七)使用JDOM修改XML文件内容

    JDOM提供了非常灵活的方式操作XML文件,使用JDOM非常简单而且代码简洁可读性强.前面我们学习了如何使用JDOM解析XML文件,本节介绍如何使用JDOM修改XML文件内容. 在这个教程中,我们准备 ...

  9. java使用SAX接口解析xml文件

    全栈工程师开发手册 (作者:栾鹏) java教程全解 java使用SAX接口解析xml文件.实现解析类,从xml解析开始到结尾,每一步均会触发对象的重载函数. 测试代码,将book1.xml改成你自己 ...

最新文章

  1. cisco2950交换机密码恢复
  2. oracle归档日志是否启用,oracle归档日志满了,导致无法启动 ORA-03113
  3. LazyProphet:使用 LightGBM 进行时间序列预测
  4. 【WPF】代码触发Button点击事件
  5. 51单片机驱动ds12887c语言,51单片机+DS12887+12864大数字时钟程序+电路
  6. Linux 文件编辑命令 详细整理
  7. 苹果自研5G调制解调器将在2023年量产 采用台积电4nm工艺
  8. 95-872-058-源码-CEP-CEP规则匹配
  9. 程序员的内功修炼——值得看的9本书
  10. 博文视点大讲堂第33期——如何让你既能干得漂亮又说得清楚?
  11. PHP处理海量样本相似度聚类算法
  12. python 获取各pkg版本_Python pkg_resources.get_distribution方法代碼示例
  13. Android studio最新版2021安装教程超详细。
  14. 清橙A1206 小Z的袜子(莫队算法)
  15. oracle数据库按日期查询,关于Oracle数据库日期范围查询的两种实现方法详解,oracle详解...
  16. MFC CListCtrl修改表头字体、字体颜色、背景颜色、字体垂直居中、表头高度
  17. c语言dht网络爬虫,用Node.js实现一个DHT网络爬虫,一步一步完成一个BT搜索引擎(一)...
  18. 权威SSL证书的CA机构有哪些
  19. 7CCSMCMP: Coursework 2 Computer Programming for Data Scientists The 7CCSMCMP instructors
  20. Mac 备份 time machine开启全速备份

热门文章

  1. 没有一点虚荣心——王利芬为马云传记写的序
  2. nc 文件的nan识别
  3. 概论第6章_正态总体的抽样分布_样本均值的期望与样本方差的期望__方差的期望
  4. 官方整顿网盘限速,网友齐刷刷@百度网盘
  5. 回撤率 python 平台_python怎么计算最大回撤
  6. 关闭 transparent_hugepage 参数
  7. x265-1.7版本-encoder/encoder.cpp注释
  8. APP种树微信小程序源码下载-简单快速上手
  9. C语言记录支出的程序,家庭支出管理系统—c语言程序设计教案.doc
  10. Visual C++网络编程经典案例详解 第9章 实用播放器 数据读取与播放控制 识别数据文件信息