OAF_文件系列6_实现OAF导出XML文件javax.xml.parsers/transformer(案例)

 20150803 Created By BaoXinjian

一、摘要


通过javax.xml.parsers和javax.xml.parsers产生XML文件

此过程需要用到一些包方法

1. File:创建XML文件

2. DocumentBuilderFactory / DocumentBuilder : 创建XML文件,并创建根节点

3. 创建节点

3.1 document.createElement:创建节点

3.2 document.createTextNode("INTERAL"):设置节点文本

3.3 element.appendChild(empnum):设置节点属性

 

二、实现分析


1. 创建exportEmployee.java程序

 

 

2. 实现解析的主要过程

package bxj.oracle.apps.ap.java;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; 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; import org.w3c.dom.Text; public class exportEmployeeXML { private void ExportEmployeeXML() { try { //创建文件 File empxmlfile = new File("/u2/VIS/visappl/gavin/employeexml/employeewxml.xml"); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); //创建根节点 Element root = doc.createElement("Header"); doc.appendChild(root); //创建EMPLOYEE节点 Element emp = doc.createElement("EMPLOYEE"); root.appendChild(emp); //创建EMPLOYEE明细节点 Element empid = doc.createElement("EMPLOYEEID"); emp.appendChild(empid); Text empid_text = doc.createTextNode("1"); empid.appendChild(empid_text); Element empnum = doc.createElement("EMPLOYEENUM"); emp.appendChild(empnum); Text empnum_text = doc.createTextNode("FS_0001"); empnum.appendChild(empnum_text); Element empname = doc.createElement("EMPLOYEENAME"); empname.setAttribute("GENDER","MALE"); emp.appendChild(empname); Text empname_text = doc.createTextNode("BaoXinjian"); empname.appendChild(empname_text); Element emptype = doc.createElement("EMPLOYEETYPE"); emp.appendChild(emptype); Text emptype_text = doc.createTextNode("INTERAL"); emptype.appendChild(emptype_text); Element empcountry = doc.createElement("COUNTRY"); emp.appendChild(empcountry); Text empcountry_text = doc.createTextNode("CN"); empcountry.appendChild(empcountry_text); Element empaddr = doc.createElement("ADDRESS"); emp.appendChild(empaddr); Text empaddr_text = doc.createTextNode("Hangzhou"); empaddr.appendChild(empaddr_text); Element empdesc = doc.createElement("DESCRIPTION"); emp.appendChild(empdesc); Text empdesc_text = doc.createTextNode("Test Employee XML Export"); empdesc.appendChild(empdesc_text); //打开文件流 FileOutputStream fos = new FileOutputStream(empxmlfile); OutputStreamWriter osw = new OutputStreamWriter(fos); callDomWriter(doc, osw, "UTF-8"); osw.close(); fos.close(); } catch (Exception ex) { ex.printStackTrace(); } } //文件转换 public void callDomWriter(Document doc, Writer writer, String encoding) { try { Source source = new DOMSource(doc); Result res = new StreamResult(writer); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); xformer.transform(source, res); }catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } public exportEmployeeXML() { } public static void main(String[] args) { System.out.println("gavin main Start"); exportEmployeeXML exportEmployeeXML = new exportEmployeeXML(); exportEmployeeXML.ExportEmployeeXML(); System.out.println("gavin main End"); } }

 

三、运行测试


1. 运行测试

 

 2. 确定是否在本地所创建的XML文件

 

3. 查看所常见的XML文件,并无问题

 

Thanks and Regards

posted on 2015-05-06 20:20  东方瀚海  阅读(351)  评论(0编辑  收藏  举报