20150803 Created By BaoXinjianjava
1、摘要oracle
經過javax.xml.parsers和javax.xml.parsers產生XML文件
app
此過程須要用到一些包方法dom
1. File:建立XML文件 測試
2. DocumentBuilderFactory / DocumentBuilder : 建立XML文件,並建立根節點ui
3. 建立節點
spa
3.1 document.createElement:建立節點code
3.2 document.createTextNode("INTERAL"):設置節點文本orm
3.3 element.appendChild(empnum):設置節點屬性xml
2、實現分析
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"); } }
3、運行測試
1. 運行測試
2. 肯定是否在本地所建立的XML文件
3. 查看所常見的XML文件,並沒有問題
Thanks and Regards