Java 怎么将JAXB转换为XML

2019年7月21日19:57:36Java 怎么将JAXB转换为XML已关闭评论 446 1187字阅读3分57秒

1.简介

Java 怎么将JAXB转换为XML?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main{
    public static String convertJAXBToXML(Object o, String context) {
        StringWriter sw = new StringWriter();
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(context);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty("jaxb.formatted.output", true);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

            marshaller.marshal(o, sw);
            LogUtility.logInfo("Converted JAXB Object "
                    + o.getClass().getSimpleName() + " to XML :  \n"
                    + sw.toString());

        } catch (JAXBException e) {
            LogUtility.logFine(e.getMessage());
        }

        return sw.toString();
    }
    public static void convertJAXBToXML(JAXBElement document) {
        Class<?> clazz = document.getValue().getClass();
        StringWriter sw = new StringWriter();
        try {
            JAXBContext context = JAXBContext.newInstance(clazz
                    .getPackage().getName());
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(document, sw);
            LogUtility.logFine(sw.toString());
        } catch (JAXBException e) {
            LogUtility.logFine(e.getMessage());
        }
    }
}

 
继续阅读
编程十万个怎么办