java实现编排和反编排_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java实现编排和反编排

java实现编排和反编排

 2014/6/27 21:32:36  xuweilovejava  程序员俱乐部  我要评论(0)
  • 摘要:新建User类@XmlRootElement(name="User")publicclassUser{privateintid;privateStringusername;privateStringnickname;privateStringpassword;privateAddressaddress;publicintgetId(){returnid;}publicStringgetUsername(){returnusername;}publicStringgetNickname()
  • 标签:实现 Java
新建User类
@XmlRootElement(name="User")
public class User {

private int id;
private String username;
private String nickname;
private String password;


private Address address;

public int getId() {
return id;
}

public String getUsername() {
return username;
}

public String getNickname() {
return nickname;
}

public String getPassword() {
return password;
}

public void setId(int id) {
this.id = id;
}

public void setUsername(String username) {
this.username = username;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public void setPassword(String password) {
this.password = password;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

}

测试类:

public class marshaller {

public static void main(String[] args) {

try {
JAXBContext jaxbContent = JAXBContext.newInstance(User.class);
Marshaller mar = jaxbContent.createMarshaller();
User u = new User();
u.setId(1);
u.setUsername("xuwei");
u.setNickname("徐伟");
u.setPassword("123456");
Address address = new Address();
address.setAddress("江西省");
u.setAddress(address);
mar.marshal(u, System.out);


String unmarStr = "<User><address><address>江西省</address></address><id>1</id><nickname>徐伟</nickname><password>123456</password><username>xuwei</username></User>";
Unmarshaller unmar = jaxbContent.createUnmarshaller();
StringReader reader = new StringReader(unmarStr);
User us = (User) unmar.unmarshal(reader);
System.out.println(us.getNickname());
Address uaddr = us.getAddress();
System.out.println(uaddr.getAddress());

} catch (JAXBException e) {
e.printStackTrace();
}

}

}
  • 相关文章
发表评论
用户名: 匿名