Googler

两情相悦,又岂在朝朝暮暮。

java 11 已移除 javax.xml.bind

    @SneakyThrows
    public static <T> String convertToXml(T obj) {
        require(obj);

        JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        //marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // pretty
        //marshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1"); // specify encoding
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        marshaller.marshal(obj, stream);
        return stream.toString(Contract.Utf8);
    }

    @SneakyThrows
    public static <T> T convertFromXml(String xml, Class<T> type) {
        require(xml, type);

        JAXBContext jaxbContext = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        byte[] data = xml.getBytes(Contract.Utf8);
        ByteArrayInputStream stream = new ByteArrayInputStream(data);
        return (T) unmarshaller.unmarshal(stream);
    }

 

posted on 2019-12-10 10:23  RockyLOMO  阅读(1746)  评论(0编辑  收藏  举报

导航

Apple/苹果笔记本 Mac Air MC968CH/A 行货在保 I5 11寸 超级本