w3c.dom组件写xml文件实例
来源:互联网 发布:sql注入原理c 编辑:程序博客网 时间:2024/05/17 08:06
w3c.dom组件写xml文件实例
package com.yanek.demo.xml.test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class W3cDomWriteXml {
/**
* @param args
*/
public static void main(String[] args) {
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.newDocument();
Element root = doc.createElement("actions");
doc.appendChild(root);
Element action1 = doc.createElement("action");
Element action2 = doc.createElement("action");
root.appendChild(action1);
root.appendChild(action2);
action1.setAttribute("path","/test");
action2.setAttribute("path","/user");
action2.setAttribute("class","com.yanek.mystruts.demo.UserAction");
action1.setAttribute("class","com.yanek.mystruts.demo.TestAction");
Element forward1 = doc.createElement("forward");
Element forward2 = doc.createElement("forward");
Element forward3 = doc.createElement("forward");
Element forward4 = doc.createElement("forward");
action1.appendChild(forward1);
action1.appendChild(forward2);
action2.appendChild(forward3);
action2.appendChild(forward4);
forward1.setAttribute("name","test");
forward2.setAttribute("name","failure");
forward1.setAttribute("url","test.jsp");
forward2.setAttribute("url","failure.jsp");
forward3.setAttribute("name","user");
forward4.setAttribute("name","failure");
forward3.setAttribute("url","list.jsp");
forward4.setAttribute("url","failure.jsp");
File file = new File("d://type111.xml");
if(!file.exists()) {
file.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
//将内存里的dom 转为xml文件
TransformerFactory tff =TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.setOutputProperty("encoding", "utf-8");
tf.transform(new DOMSource(doc), new StreamResult(bw));
bw.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
生成xml文件:
<?xml version="1.0" encoding="utf-8"?><actions><action class="com.yanek.mystruts.demo.TestAction" path="/test"><forward name="test" url="test.jsp"/><forward name="failure" url="failure.jsp"/></action><action class="com.yanek.mystruts.demo.UserAction" path="/user"><forward name="user" url="list.jsp"/><forward name="failure" url="failure.jsp"/></action></actions>
- w3c.dom组件写xml文件实例
- w3c.dom组件写xml文件实例
- w3c.dom组件xml解析实例
- w3c.dom组件xml解析实例
- w3c.dom 解析xml
- w3c dom创建xml
- java解析、读取xml文件,org.w3c.dom.Document
- org.w3c.dom 对XML 文件的操作
- org.w3c.dom 遍历xml文件的两种方法
- java 使用org.w3c.dom操作XML文件
- dom 解析xml文件实例
- DOM解析XML文件实例
- XHTML XML DTD DOM W3C
- ORG.W3C.DOM操作XML
- 解析xml--org.w3c.dom
- java w3c dom 解析 xml
- Python:Dom生成XML文件(写XML)
- Python:Dom生成XML文件(写XML)
- 工厂模式(Factory Pattern) 一
- kindle取消注册与注册教程
- VLC网络通信流程分析
- 在学习HTML5时,别忘了CSS技术
- ASP.NET MVC3 快速入门-第九节 实现编辑、删除与明细信息视图
- w3c.dom组件写xml文件实例
- Ubuntu下修改系统默认语言为简体中文
- Android小技巧之统一DebugKey
- 雷锋微视点:Facebook竟以股价论英雄
- w3c.dom组件xml解析实例
- 本地破解WINDOWS XP 系统的登陆密码
- 小问题,大麻烦
- MYSQL数据库添删改查—操作过程记录
- WinCe+WebService