很多时候我们需要把认知世界转化为我们熟悉的java对象,以供方便操作。这里,JAXB可以把xml对象转化为我们的java对象,也可以把java对象转化为xml对象。这时候我们就得知道它的两个转化方法。
一个是unmarshal(),一个是marshal()
unmarshal()是把xml对象转化为我们需要的java对象的方法,自然marshal()是把java对象转化为xml对象的一个过程。
<?xml version="1.0" encoding="utf-8"?>
<root>
<girl city="深圳">
<name>zs</name>
<age>1</age>
</girl>
<boy>
<name>ls</name>
<age>1</age>
</boy>
</root>
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
public class XmlUtil {
@SuppressWarnings({ "restriction", "unchecked" })
public static <T> T toObject(File file, Class<T> type) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance(type);
return (T) jc.createUnmarshaller().unmarshal(file);
}
@SuppressWarnings({ "restriction", "unchecked" })
public static <T> T toObject(String context, Class<T> type) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance(type);
return (T) jc.createUnmarshaller().unmarshal(new StringReader(context));
}
@SuppressWarnings("restriction")
public static void toXml(Object object, String FilePath) throws JAXBException, IOException {
JAXBContext jc = JAXBContext.newInstance(object.getClass());
OutputStream outputStream = new FileOutputStream(new File(FilePath));
jc.createMarshaller().marshal(object, outputStream);
outputStream.close();
}
}
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
class People {
@XmlElement(name = "name")
private String name;
@XmlElement(name = "age")
private int age;
@XmlAttribute(name = "city")
private String city;
public People() {
}
public People(String name, int age) {
this.name = name;
this.age = age;
}
public People(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}
@Override
public String toString() {
return name + "," + age + "," + city + "\n";
}
public int getAge() {
return age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "root")
class Root {
@XmlElement(name = "girl")
private List<People> girls;
@XmlElement(name = "boy")
private List<People> boys;
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (People girl : girls) {
sb.append(girl.toString());
}
for (People girl : boys) {
sb.append(girl.toString());
}
return sb.toString();
}
public List<People> getGirls() {
return girls;
}
public void setGirls(List<People> girls) {
this.girls = girls;
}
public List<People> getBoys() {
return boys;
}
public void setBoys(List<People> boys) {
this.boys = boys;
}
}
public static void main(String[] args) throws JAXBException, IOException {
Root root = new Root();
List<People> list = new ArrayList<People>();
list.add(new People("zs", 1, "深圳"));
root.setGirls(list);
list = new ArrayList<People>();
list.add(new People("ls", 1));
root.setBoys(list);
XmlUtil.toXml(root, "F:\\t.xml");
root = XmlUtil.toObject(new File("F:\\t.xml"), root.getClass());
System.out.println(root);
}
从最里层开始(girl),把我们想要的节点标记好了:@XmlElement(name = “xxx”),并且给出他的get,set方法。
同时,要给Girl这个class加上@XmlAccessorType(XmlAccessType.FIELD) ,表示使用这个类中的 private 非静态字段作为 XML 的序列化的属性或者元素。 也就是说,咱们把里边的Girl类中的name来表示xml文件中的name,非常简单。当然,我们需要在name的头上加一顶小帽子@XmlElement(name = “name”)