首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

Java 写xml资料

2012-12-19 
Java 写xml文件package com.biztoolimport java.io.Fileimport java.io.FileOutputStreamimport java.i

Java 写xml文件

package com.biztool;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 WriteXml {private void writeXMLFile(String xmlfile) {File file = new File("E:/tryfile/xml");file.listFiles();if(!file.exists()) {file.mkdirs();}DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();DocumentBuilder dbuilder = null;try {dbuilder = dbf.newDocumentBuilder();}catch(Exception ex) {ex.printStackTrace();}Document doc = dbuilder.newDocument();Element root = doc.createElement("学生花名册");doc.appendChild(root);Element stu = doc.createElement("学生");stu.setAttribute("性别","男");root.appendChild(stu);Element stu_name = doc.createElement("姓名");stu.appendChild(stu_name);Text name_text = doc.createTextNode("罗文雯");stu_name.appendChild(name_text);Element stu_age = doc.createElement("年龄");stu.appendChild(stu_age);Text age_text = doc.createTextNode("25");stu_age.appendChild(age_text);try {FileOutputStream fos = new FileOutputStream(xmlfile+"/test.xml");OutputStreamWriter osw = new OutputStreamWriter(fos);callDomWriter(doc, osw, "UTF-8");osw.close();fos.close();}catch(Exception ex) {ex.printStackTrace();}}public void callDomWriter(Document dom , Writer writer, String encoding) {try {Source source = new DOMSource(dom);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 static void main(String []args) {WriteXml writeins = new WriteXml();String outfile = "E:/tryfile/xml";writeins.writeXMLFile(outfile);System.out.println("finished");}}


 

热点排行