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>

目录
相关文章
|
15天前
|
XML 前端开发 Java
讲解SSM的xml文件
本文详细介绍了SSM框架中的xml配置文件,包括springMVC.xml和applicationContext.xml,涉及组件扫描、数据源配置、事务管理、MyBatis集成以及Spring MVC的视图解析器配置。
37 1
|
2月前
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
16天前
|
XML JavaScript Java
java与XML文件的读写
java与XML文件的读写
16 3
|
1月前
|
XML JavaScript 前端开发
学习react基础(1)_虚拟dom、diff算法、函数和class创建组件
本文介绍了React的核心概念,包括虚拟DOM、Diff算法以及如何通过函数和类创建React组件。
20 2
|
18天前
|
XML 存储 缓存
C#使用XML文件的详解及示例
C#使用XML文件的详解及示例
44 0
|
18天前
|
XML 存储 Web App开发
查看 XML 文件
查看 XML 文件
|
1月前
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
39 1
|
2月前
|
XML 数据格式
【Azure Logic App】在Logic App中使用 Transfer XML组件遇见错误 undefined
【Azure Logic App】在Logic App中使用 Transfer XML组件遇见错误 undefined
|
3月前
|
XML Java 数据库
配置applicationContext.xml文件
配置applicationContext.xml文件
|
3月前
|
XML Java 数据格式
支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法
支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法