国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java使用Jdom實現xml文件寫入操作實例

2019-11-26 14:53:32
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 施甸县| 墨脱县| 二手房| 栾城县| 宁陕县| 鸡泽县| 宿迁市| 稷山县| 来安县| 登封市| 怀宁县| 慈利县| 抚松县| 加查县| 巢湖市| 玛沁县| 会同县| 锦州市| 罗平县| 郑州市| 屯昌县| 嘉黎县| 新巴尔虎右旗| 临沂市| 石门县| 伊金霍洛旗| 东平县| 高雄县| 宿松县| 张掖市| 敦煌市| 崇义县| 台江县| 济南市| 乐东| 汤原县| 上林县| 陕西省| 乐昌市| 泸溪县| 高密市|