package com.demo; import com.test.JAXBDateFromater; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import java.time.LocalDate; import java.util.List; @XmlRootElement public class Person { private String name; private String email; private String password; private String phone; private List<String> list; private Integer age; private LocalDate birthday; public Person() { } @XmlElement(name = "zh_name") public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlAttribute public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @XmlElementWrapper(name = "list") @XmlElement(name="addr") public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @XmlJavaTypeAdapter(JAXBDateFromater.class) public LocalDate getBirthday() { return birthday; } public void setBirthday(LocalDate birthday) { this.birthday = birthday; } }
package com.test; import javax.xml.bind.annotation.adapters.XmlAdapter; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Optional; public class JAXBDateFromater extends XmlAdapter<String,LocalDate>{ private static final String formater="yyyy/MM/dd"; @Override public LocalDate unmarshal(String v) throws Exception { Optional<String> optional=Optional.ofNullable(v); if(optional.isPresent()){ return null; } LocalDate localDate = LocalDate.parse(v, DateTimeFormatter.ofPattern(formater)); return localDate; } @Override public String marshal(LocalDate v) throws Exception { String format = v.format(DateTimeFormatter.ofPattern(formater)); return format; } }
package com.test; import com.demo.Person; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import java.io.StringWriter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.LocalDate; import java.time.Month; import java.util.Arrays; public class Test { public static void main(String arg[]) { try { JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8"); Person person=new Person(); person.setList(Arrays.asList("广东","上海")); person.setAge(23); person.setBirthday(LocalDate.of(1990, Month.of(12),20)); person.setEmail("16kkk@qq.com"); MessageDigest md5 = MessageDigest.getInstance("md5"); md5.update("kkkkkk".getBytes()); byte[] digest = md5.digest(); person.setPassword(new String(digest)); StringWriter stringWriter=new StringWriter(); marshaller.marshal(person,stringWriter); System.out.println(stringWriter.toString()); } catch (JAXBException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } }
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person email="16kkk@qq.com">
<age>23</age>
<birthday>1990/12/20</birthday>
<list>
<addr>广东</addr>
<addr>上海</addr>
</list>
<password>���j� Uf��T˾�_�</password>
</person>