加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

输出数据到xml文件(java实现)

发布时间:2020-12-16 06:08:37 所属栏目:百科 来源:网络整理
导读:package com.xiuye.utils;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Random;import org.dom4j.Document;impor
package com.xiuye.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Random;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class OutputEmpListXml {

	public static void main(String[] args) {

		// prepare char's array to generate random name
		char letters[] = new char[54];
		letters[0] = ' ';
		letters[1] = '-';
		// a~z
		for (int i = 0 + 2,j = 0; i < 26 + 2 && j < 26; i++,j++) {
			letters[i] = (char) ('a' + j);
		}
		// A~Z
		for (int i = 0 + 2 + 26,j = 0; i < 26 + 2 + 26 && j < 26; i++,j++) {
			letters[i] = (char) ('A' + j);
		}

		String sexs[] = { "man","woman" };

		Random rnd = new Random();
		// the whole xml file
		Document doc = DocumentHelper.createDocument();
		/**
		 * only one root node,if not,IllegalAddException
		 * 
		 */
		// node root
		Element root = doc.addElement("emp-list");
		for (int i = 0; i < 1000; i++) {
			
			
			// node emp
			Element emp = root.addElement("emp");
			int id = rnd.nextInt(9999999);
			// emp's attribute id
			emp.addAttribute("id",Integer.toString(id));
			// node name
			Element name = emp.addElement("name");
			name.setText(generateRandomName(letters));
			// node age
			Element age = emp.addElement("age");
			// node geneder
			Element geneder = emp.addElement("geneder");
			// node salary
			Element salary = emp.addElement("salary");
			// random age
			int ageInt = rnd.nextInt(100);
			age.setText(Integer.toString(ageInt));// first way "int -> String"
			// random sex
			int index = rnd.nextInt(2);
			geneder.setText(sexs[index]);
			// random salary
			int money = rnd.nextInt(10000000);
			salary.setText("" + money);// second way "int -> String"
		}
		
		
		try {
			FileOutputStream out = new FileOutputStream("EmpList.xml");
			OutputFormat format = OutputFormat.createPrettyPrint();
			XMLWriter xmlw = new XMLWriter(out,format);
			xmlw.write(doc);
			
			xmlw.close();
			
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		System.out.println("that's over!");
		
		
	}

	// get a simple random name
	private static String generateRandomName(char[] letters) {
		//String name = null;
		/**
		 * name cannot be bull,beacause of 
		 * null += "ABC";=> nullABC
		 * it's not my wanted.
		 * 
		 */
		String name = "";
		int nameLength = (int) (Math.random() * letters.length) + 1;
		for (int i = 0; i < nameLength; i++) {
			int index = (int) (Math.random() * letters.length);
			name += "" + letters[index];
		}

		return name;

	}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读