w3c.dom组件写xml文件实例

简介:

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>

目录
相关文章
|
5天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
8 0
|
7天前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
26天前
|
XML C# 数据格式
使用C#操作XML文件
使用C#操作XML文件
11 0
|
27天前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
9 0
|
28天前
|
Kubernetes Cloud Native Java
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
37 1
|
27天前
|
XML Java 数据库连接
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
10 0
|
22天前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
14 1
|
26天前
|
XML Java 数据格式
使用java解析XML文件的步骤
使用java解析XML文件的步骤
10 0
|
1月前
|
SQL Java 数据库连接
Mybatis中Mapper.xml 文件使用注释遇到的坑
Mybatis中Mapper.xml 文件使用注释遇到的坑
|
Web App开发 JavaScript
W3C对DOM2.0定义的标准事件
DOM2.0模型将事件处理流程分为三个阶段:   一、事件捕获阶段,   二、事件目标阶段,   三、事件起泡阶段。 具体如图(图片来源于网络,侵删) 事件捕获:当某个元素触发某个事件(如onclick),顶层对象document就会发出一个事件流,随着DOM树的节点向目标元素节点流去,直到到达事件真正发生的目标元素。
956 0