---
title: "Java读写xml（对简单关系，无需中间模型）"
author: "lylegend13"
date: "2011-10-11T11:26:52Z"
tags: []
categories: ["编程语言"]
slug: "java-xml"
draft: false
lastmod: "2011-10-12T16:03:55Z"
id: 1189440
url: "https://lltobe.iteye.com/blog/1189440"
---

```
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 stu = new stu();
			stu.pet = new ArrayList<String>();
			stu.pet.add("y1");
			stu.pet.add("x2");
			stus.stu.add(stu);

			stu = new stu();
			stu.pet = new ArrayList<String>();
			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> stu;
}

class stu {
	public List<String> pet;
}
```

说明：

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

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

 

```
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<stus>
	<stu>
		<pet>y1</pet>
		<pet>x2</pet>
	</stu>
	<stu>
		<pet>y2</pet>
		<pet>x1</pet>
		<pet>z3</pet>
	</stu>
</stus>
```

 

输出结果：

\[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 stu = new stu();
			// stu.pet = new ArrayList<String>();
			stu.pet.add("y1");
			stu.pet.add("x2");
			stus.stu.add(stu);

			stu = new stu();
			// stu.pet = new ArrayList<String>();
			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> stu = new ArrayList<stu>();
}

class stu {
	public List<String> pet = new ArrayList<String>();
}
```