JAXB生成xml对时间的处理

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>
 

转载于:https://my.oschina.net/u/3484671/blog/1616438

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值