2013年4月29日 星期一

[ Java 代碼範本 ] How To Create XML File In Java – (DOM Parser)

來源自 這裡 
Preface: 
DOM provides many handy classes to create XML file easily. Firstly, you have to create a Document with DocumentBuilder class, define all the XML content – node, attribute with Element class. In last, use Transformer class to output the entire XML content to stream output, typically a File. 

In this tutorial, we show you how to use DOM XML parser to create a XML file. 

DOM Parser Example: 
At the end of the example, following XML file named “file.xml” will be created: 
 

範例代碼如下: 
- WriteXMLFile.java – Java class to create a XML file. 
  1. package com.mkyong.core;  
  2.   
  3. import java.io.File;  
  4. import javax.xml.parsers.DocumentBuilder;  
  5. import javax.xml.parsers.DocumentBuilderFactory;  
  6. import javax.xml.parsers.ParserConfigurationException;  
  7. import javax.xml.transform.Transformer;  
  8. import javax.xml.transform.TransformerException;  
  9. import javax.xml.transform.TransformerFactory;  
  10. import javax.xml.transform.dom.DOMSource;  
  11. import javax.xml.transform.stream.StreamResult;  
  12.   
  13. import org.w3c.dom.Attr;  
  14. import org.w3c.dom.Document;  
  15. import org.w3c.dom.Element;  
  16.   
  17. public class WriteXMLFile {  
  18.   
  19.     public static void main(String argv[]) {  
  20.   
  21.       try {  
  22.   
  23.         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();  
  24.         DocumentBuilder docBuilder = docFactory.newDocumentBuilder();  
  25.   
  26.         // root elements  
  27.         Document doc = docBuilder.newDocument();  
  28.         Element rootElement = doc.createElement("company");  
  29.         doc.appendChild(rootElement);  
  30.   
  31.         // staff elements  
  32.         Element staff = doc.createElement("Staff");  
  33.         rootElement.appendChild(staff);  
  34.   
  35.         // set attribute to staff element  
  36.         Attr attr = doc.createAttribute("id");  
  37.         attr.setValue("1");  
  38.         staff.setAttributeNode(attr);  
  39.   
  40.         // shorten way  
  41.         // staff.setAttribute("id", "1");  
  42.   
  43.         // firstname elements  
  44.         Element firstname = doc.createElement("firstname");  
  45.         firstname.appendChild(doc.createTextNode("yong"));  
  46.         staff.appendChild(firstname);  
  47.   
  48.         // lastname elements  
  49.         Element lastname = doc.createElement("lastname");  
  50.         lastname.appendChild(doc.createTextNode("mook kim"));  
  51.         staff.appendChild(lastname);  
  52.   
  53.         // nickname elements  
  54.         Element nickname = doc.createElement("nickname");  
  55.         nickname.appendChild(doc.createTextNode("mkyong"));  
  56.         staff.appendChild(nickname);  
  57.   
  58.         // salary elements  
  59.         Element salary = doc.createElement("salary");  
  60.         salary.appendChild(doc.createTextNode("100000"));  
  61.         staff.appendChild(salary);  
  62.   
  63.         // write the content into xml file  
  64.         TransformerFactory transformerFactory = TransformerFactory.newInstance();  
  65.         Transformer transformer = transformerFactory.newTransformer();  
  66.         DOMSource source = new DOMSource(doc);  
  67.         StreamResult result = new StreamResult(new File("C:\\file.xml"));  
  68.   
  69.         // Output to console for testing  
  70.         // StreamResult result = new StreamResult(System.out);  
  71.   
  72.         transformer.transform(source, result);  
  73.   
  74.         System.out.println("File saved!");  
  75.   
  76.       } catch (ParserConfigurationException pce) {  
  77.         pce.printStackTrace();  
  78.       } catch (TransformerException tfe) {  
  79.         tfe.printStackTrace();  
  80.       }  
  81.     }  
  82. }  
A new XML file is created in “C:\\file.xml“, with default UTF-8 encoded. For debugging, you can change the StreamResult to output the XML content to your console. 
  1. StreamResult result =  new StreamResult(System.out);  
  2. transformer.transform(source, result);  
Supplement: 
Java中四種操作(DOM、SAX、JDOM、DOM4J)xml方式的比較與詳解

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...