JAXB完成对象与XML之间的转换
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.fool.xml</groupId><artifactId>xml</artifactId><version>0.0.1-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><junit-version>4.11</junit-version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit-version}</version><scope>test</scope></dependency></dependencies></project>
?
2.Project DirectoryGroup.java
package org.fool.xml.jaxb;public class Group{private int id;private String name;public Group(){}public Group(int id, String name){this.id = id;this.name = name;}public int getId(){return id;}public void setId(int id){this.id = id;}public String getName(){return name;}public void setName(String name){this.name = name;}@Overridepublic String toString(){return "Group [id=" + id + ", name=" + name + "]";}}
User.java
package org.fool.xml.jaxb;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class User{private int id;private String username;private String password;private String nickname;private Group group;public User(){}public User(int id, String username, String password, String nickname,Group group){this.id = id;this.username = username;this.password = password;this.nickname = nickname;this.group = group;}public int getId(){return id;}public void setId(int id){this.id = id;}public String getUsername(){return username;}public void setUsername(String username){this.username = username;}public String getPassword(){return password;}public void setPassword(String password){this.password = password;}public String getNickname(){return nickname;}public void setNickname(String nickname){this.nickname = nickname;}public Group getGroup(){return group;}public void setGroup(Group group){this.group = group;}@Overridepublic String toString(){return "User [id=" + id + ", username=" + username + ", password="+ password + ", nickname=" + nickname + ", group=" + group+ "]";}}
?
4.src/test/javaTestJaxb.java
package org.fool.xml.test;import java.io.File;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;import org.fool.xml.jaxb.Group;import org.fool.xml.jaxb.User;import org.junit.Test;public class TestJaxb{@Testpublic void testJavaToXML(){try{JAXBContext ctx = JAXBContext.newInstance(User.class);Marshaller marshaller = ctx.createMarshaller();Group group = new Group(1, "Develop");User user = new User(1, "JAXB", "JAXB", "XML", group);marshaller.marshal(user, new File("jaxb.xml"));}catch (JAXBException e){e.printStackTrace();}}@Testpublic void testXMLtoJava(){try{JAXBContext ctx = JAXBContext.newInstance(User.class);Unmarshaller unmarshaller = ctx.createUnmarshaller();User user = (User) unmarshaller.unmarshal(new File("jaxb.xml"));System.out.println(user);}catch (JAXBException e){e.printStackTrace();}}}
?
5.ResulttestJavaToXML
jaxb.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><user><group><id>1</id><name>Develop</name></group><id>1</id><nickname>XML</nickname><password>JAXB</password><username>JAXB</username></user>
testXMLtoJava
console output
User [id=1, username=JAXB, password=JAXB, nickname=XML, group=Group [id=1, name=Develop]]
?
?
?