Java读写xml(对简单关系,无需中间模型)

阅读更多
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

public class test2 {

	public static void main(String[] args) {
		write();

		read();
	}

	public static void write() {
		try {
			JAXBContext jc = JAXBContext.newInstance(stus.class);
			Marshaller u = jc.createMarshaller();

			stus stus = new stus();
			stus.stu = new ArrayList();

			stu stu = new stu();
			stu.pet = new ArrayList();
			stu.pet.add("y1");
			stu.pet.add("x2");
			stus.stu.add(stu);

			stu = new stu();
			stu.pet = new ArrayList();
			stu.pet.add("y2");
			stu.pet.add("x1");
			stu.pet.add("z3");
			stus.stu.add(stu);

			u.marshal(stus, new File("d:\\2.xml"));
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}

	public static void read() {
		try {
			JAXBContext jc = JAXBContext.newInstance(stus.class);
			Unmarshaller u = jc.createUnmarshaller();

			stus stus = (stus) u.unmarshal(new File("d:\\2.xml"));

			for (stu stu : stus.stu) {
				System.out.println(stu.pet);
			}
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
}

@XmlRootElement
class stus {
	public List stu;
}

class stu {
	public List pet;
}

说明:

1、java读写xml时,java中对象结构可以跟xml中的对象结构完全一致。

2、java中的对象类只需要基本的包含结构,不需要依赖任何方法。 

 



	
		y1
		x2
	
	
		y2
		x1
		z3
	

 

输出结果:

[y1, x2]
[y2, x1, z3]

-------------------------------------------------------------------------------

 

 

stus/stu类中的List也可以初始化,于是代码如下:

 

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

public class test2 {

	public static void main(String[] args) {
		write();

		read();
	}

	public static void write() {
		try {
			JAXBContext jc = JAXBContext.newInstance(stus.class);
			Marshaller u = jc.createMarshaller();

			stus stus = new stus();
			// stus.stu = new ArrayList();

			stu stu = new stu();
			// stu.pet = new ArrayList();
			stu.pet.add("y1");
			stu.pet.add("x2");
			stus.stu.add(stu);

			stu = new stu();
			// stu.pet = new ArrayList();
			stu.pet.add("y2");
			stu.pet.add("x1");
			stu.pet.add("z3");
			stus.stu.add(stu);

			u.marshal(stus, new File("d:\\2.xml"));
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}

	public static void read() {
		try {
			JAXBContext jc = JAXBContext.newInstance(stus.class);
			Unmarshaller u = jc.createUnmarshaller();

			stus stus = (stus) u.unmarshal(new File("d:\\2.xml"));

			for (stu stu : stus.stu) {
				System.out.println(stu.pet);
			}
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
}

@XmlRootElement
class stus {
	public List stu = new ArrayList();
}

class stu {
	public List pet = new ArrayList();
}

 

你可能感兴趣的:(Java读写xml(对简单关系,无需中间模型))