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

XML 生成种

2012-09-21 
XML 生成类package com.zyna.dbstructureimport java.io.Writerimport javax.xml.parsers.DocumentBuild

XML 生成类

package com.zyna.dbstructure;import java.io.Writer;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import javax.xml.transform.Result;import javax.xml.transform.Source;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.OutputKeys;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;public class XML {DocumentBuilderFactory builderFactory = null;DocumentBuilder builder = null ;Document document = null ;Element root = null ;/** * 初始化一切预备参数 *  * 编写者:lfc *  * @throws ParserConfigurationException */public void init() throws ParserConfigurationException {builderFactory = DocumentBuilderFactory.newInstance() ;builder = builderFactory.newDocumentBuilder() ;document = builder.newDocument() ;//*******生成根节点********//root = document.createElement("info") ;document.appendChild(root) ;}/** * 创建XML文档 *  * @param   strs  *       -------传送过来的姓名和密码和学校参数 *  * 编写者:lfc */public void create(String[] strs) {//******第一级子节点******//Element first = document.createElement("OperateTask") ;root.appendChild(first) ;for(int i=0; i<strs.length; i++) {if(i==0) {//*******第二级子节点******//Element ot_id = document.createElement("ot_id") ;ot_id.appendChild(document.createTextNode(strs[i])) ;first.appendChild(ot_id) ;} if(i==1) {Element ot_item = document.createElement("ot_item") ;ot_item.appendChild(document.createTextNode(strs[i])) ;first.appendChild(ot_item) ;} if(i==2) {Element ot_type = document.createElement("ot_type") ;ot_type.appendChild(document.createTextNode(strs[i])) ;first.appendChild(ot_type) ;}if(i == 3){Element ot_level = document.createElement("ot_level") ;ot_level.appendChild(document.createTextNode(strs[i])) ;first.appendChild(ot_level) ;}}System.out.println(first.getNodeValue());System.out.println(first.getNodeName());}public Document getDocument() {return document ;}/** * 把XML文档写入到输出流 *  * @param out *       ----指定的输出流 * @throws Exception *  * 编写者:lfc */public void output(Writer writer) throws Exception{Transformer trans = TransformerFactory.newInstance().newTransformer() ;trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8") ;Source source = new DOMSource(document) ;Result result = new StreamResult(writer) ;trans.transform(source, result) ;writer.flush() ;writer.close() ;}public static void main(String[] args) throws ParserConfigurationException{XML xML = new XML();xML.init();System.out.println(xML.document);}}

热点排行