用jaxb通过XSD生成Java类,java对象转换成xml文件

来,废话不多说,直接进入状态。


首先先使用Altova XMLSpy画一个xsd文件,或者直接使用代码写一个xsd文件,直接写代码就查查资料吧!

就用最简单的Student吧!画的Student.xsd

代码如下:




	
		
			学生
		
		
			
				
					
						
							
							
						
					
				
				
					
						
							
							
						
					
				
				
					
						
							
							
						
					
				
				
					
						
							
							
						
					
				
			
		
	

这里我使用的工具是:jaxb-ri-20120516;

运行dos,并跳转到C:\Users\BoiseWu\Desktop\jaxb-ri-20120516\bin(这是我的路径)路径下,

并把生成的Student.xsd文件放到C:\Users\BoiseWu\Desktop\jaxb-ri-20120516\bin下,

设置java类的输出路径,运行set output_path=d:\student        d:\student此路径必须存在。 

执行:xjc -d %output_path% -p com.boise.bean   -b binding.xml Student.xsd

在这java类便生成了。


下面是由java对象转化成xml文件和从xml文件转成java对象

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class ToXML {
	public static void main(String[] args) {
		Student student = new Student();
		student.setId(1111);
		student.setName("zhangsan");
		student.setAge(20);
		student.setSex("male");

		String xmlStr = createXML(Student.class, student);

		//System.out.println(xmlStr);
		//由java对象转成xml
		byte[] bytexml = xmlStr.getBytes();

		try {
			OutputStream os = new FileOutputStream(new File("d:/student.xml"));
			os.write(bytexml);
			os.flush();
			os.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//由xml转成java对象
		try{
			InputStream is = new FileInputStream(new File("d:/student.xml"));
			byte [] bytes = new byte[100];
			xmlStr = "";
			int temp;
			while((temp=is.read(bytes))!=-1){
				xmlStr +=new String(bytes,0,temp);
			}
			System.out.println(xmlStr);
			
			Student stu = (Student) createObject(Student.class, xmlStr);
			System.out.println(stu.getName());
		}catch (Exception e) {
			e.printStackTrace();
		}
		

	}

	public static  String createXML(Class entityClass, Object entity) {
		StringWriter writer = new StringWriter();
		try {
			JAXBContext act = JAXBContext.newInstance(entityClass);
			Marshaller marshaller = act.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			marshaller.marshal(entity, writer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return writer.toString();
	}

	public static  Object createObject(Class entityClass, String entity) {
		Object object = null;
		try {
			JAXBContext act = JAXBContext.newInstance(entityClass);
			Unmarshaller unMarshaller = act.createUnmarshaller();
			object = unMarshaller.unmarshal(new StringReader(entity));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}
}

哈哈,就这就OK啦!

下面是输出结果:






你可能感兴趣的:(Java)