Java Xml 工具类


import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;

/**
 * @Date 2022/4/29
 */

public class XmlUtil {

    public static final String DEFAULT_ENCODING = "UTF-8";
    public static final String VERSION = "1.0";

    /**
     * 将对象直接转换成String类型的 XML输出
     *
     * @param obj
     * @return
     */
    public static String convertToXml(Object obj) throws Exception {
        StringWriter sw = new StringWriter();
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            throw e;
        } finally {
            if (null != sw) {
                try {
                    sw.flush();
                    sw.close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }
        return sw.toString();
    }

    /**
     * 将对象直接转换成String类型的 XML输出; xml中删除standalone =“yes”
     *
     * @param obj
     * @return
     * @throws Exception
     */
    public static String ObjToXml(Object obj) throws Exception {
        XMLStreamWriter writer = null;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            XMLOutputFactory factory = XMLOutputFactory.newFactory();
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, DEFAULT_ENCODING);
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
            writer = factory.createXMLStreamWriter(
                    os, (String) marshaller.getProperty(Marshaller.JAXB_ENCODING));
            writer.writeStartDocument(
                    (String) marshaller.getProperty(Marshaller.JAXB_ENCODING), VERSION);
            marshaller.marshal(obj, writer);
            writer.writeEndDocument();
        } catch (Exception e) {
            throw e;
        } finally {
            if (null != os) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    throw e;
                }
            }
            if (null != writer) {
                try {
                    writer.flush();
                    writer.close();
                } catch (Exception e) {
                    throw e;
                }
            }
        }
        return new String(os.toByteArray());
    }


    /**
     * xml追加节点
     *
     * @param reqDate
     * @param nodeName
     * @param nodeValue
     * @return
     * @throws DocumentException
     */
    public static String addNodeToreqDate(String reqDate, String nodeName, String nodeValue) throws DocumentException {
        if (StringUtils.isBlank(nodeName) || StringUtils.isBlank(nodeValue)) {
            return reqDate;
        }
        Document document = DocumentHelper.parseText(reqDate);
        //获取文档根节点
        Element root = document.getRootElement();
        // 获取根节点下指定节点
        Element productElem = root.element("Product");
        Element attributesElem = productElem.element("Attributes");
        Element element = attributesElem.addElement(nodeName);
        element.setText(nodeValue);
        reqDate = root.asXML();
        return reqDate;
    }
}

你可能感兴趣的:(java,xml)