w3c.dom組件寫xml檔案執行個體

來源:互聯網
上載者:User

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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.