java输出xml代表的文件_输出数据到xml文件(java实现)

搜索热词

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;

}

}

相关文章

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值