java生成 xml的工具,xml java端生成 工具种

xml java端生成 工具类

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

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值