XML

这是一个自动生成XML文件的代码


package com.cyberwise.objecttoxml;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成XML文档
 * 
 * @author Lyong
 * 
 */
public class ObjectToXml {
	private final String TITLE = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
	private final String ROOT_START = "<objects>";
	private final String ROOT_END = "</objects>";
	private final String ROW_START = "<simple>";
	private final String ROW_END = "</simple>";

	public String getObjectToXml(List<Object> list) {
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(TITLE);
		stringBuffer.append(ROOT_START);
		for (int i = 0; i < list.size(); i++) {
			Object object = list.get(i);
			stringBuffer.append(ROW_START);
			Field[] fields = object.getClass().getDeclaredFields();
			for (int j = 0; j < fields.length; j++) {
				try {
					String name = fields[j].getName();

					Method method = object.getClass().getMethod(
							"get" + name.substring(0, 1).toUpperCase()
									+ name.substring(1), new Class[] {});// 这里的get就是来取值的。如果没有就只有结点而没有值啊
					Object result = method.invoke(object, new Object[] {});
					stringBuffer.append(" " + name + "=\"" + result + "\"");
				} catch (Exception e) {
					e.getStackTrace();
				}
			}
			stringBuffer.append(ROW_END);
		}
		stringBuffer.append(ROOT_END);
		return stringBuffer.toString();
	}

	public boolean witerXmlFile(String filepath, String str) {
		File file = new File(filepath);
		try {
			if (!file.exists()) {
				// TODO:文件不存在 则创建
				file.createNewFile();
			}

			BufferedWriter bufferedWriter = new BufferedWriter(
					new OutputStreamWriter(new FileOutputStream(file)));
			bufferedWriter.write(str);

			bufferedWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	public static void main(String[] args) {
		ObjectToXml objectToXml = new ObjectToXml();

		List<Object> list = new ArrayList<Object>();
		SimpleObject object = new SimpleObject();
		object.setObjectStr1("xingming");
		object.setObjectStr2("mingzi");
		object.setObjectStr3("gaoshou");
		list.add(object);

		object = new SimpleObject();
		object.setObjectStr1("xingming");
		object.setObjectStr2("mingzi");
		object.setObjectStr3("gaoshou");
		list.add(object);

		object = new SimpleObject();
		object.setObjectStr1("xingming");
		object.setObjectStr2("mingzi");
		object.setObjectStr3("gaoshou");
		list.add(object);

		String xmlResult = objectToXml.getObjectToXml(list);
		System.out.println(xmlResult);

		boolean flag = objectToXml.witerXmlFile("c:/xmlResult.xml", xmlResult);
		if (flag) {
			System.out.println("写文件成功!");
		} else {
			System.out.println("写文件失败!");
		}
	}
}

class SimpleObject {
	private String objectStr1;
	private String objectStr2;
	private String objectStr3;

	public SimpleObject() {

	}

	public String getObjectStr1() {
		return objectStr1;
	}

	public void setObjectStr1(String objectStr1) {
		this.objectStr1 = objectStr1;
	}

	public String getObjectStr2() {
		return objectStr2;
	}

	public void setObjectStr2(String objectStr2) {
		this.objectStr2 = objectStr2;
	}

	public String getObjectStr3() {
		return objectStr3;
	}

	public void setObjectStr3(String objectStr3) {
		this.objectStr3 = objectStr3;
	}
}


你可能感兴趣的:(java,xml,J#)