本文主要是介绍【开发心得】Jaxb使用珠玑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
Java操作xml转换成javaBean,或者javaBean转换为xml的方式有很多。常见的有dom4j等工具直接操作dom,或者使用jaxb.
jaxb介绍:
JAXB(Java Architecture for XML Binding简称JAXB)允许Java开发人员将Java类映射为XML表示方式。JAXB提供两种主要特性:将一个Java对象序列化为XML,以及反向操作,将XML解析成Java对象。换句话说,JAXB允许以XML格式存储和读取数据,而不需要程序的类结构实现特定的读取XML和保存XML的代码。
工具类
package com.test.tms.backend.service.utils;import com.test.tms.backend.service.xml.ParseXmlExceptionHandler;
import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;
import com.sun.xml.internal.bind.marshaller.NoEscapeHandler;
import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl;
import com.sun.xml.internal.bind.v2.util.XmlFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;import javax.xml.bind.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXSource;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;/*** @Author: martin* @Date: 2023/8/15 09:44* @Description:*/@Slf4j
public class XMLUtils {/*** 将String类型的xml转换成对象*/public static Object convertXmlStrToObject(Class clazz, String xmlStr) {Object xmlObject = null;try {JAXBContext context = JAXBContext.newInstance(clazz);Unmarshaller unmarshaller = context.createUnmarshaller();unmarshaller.setEventHandler(new ValidationEventHandler() {@Overridepublic boolean handleEvent(ValidationEvent event) {if (event.getSeverity() == 2) {Throwable t = event.getLinkedException();if (t != null) {log.error(t.getMessage(), t);}return true;}return true;}});InputStream inputStream = IOUtils.toInputStream(xmlStr, StandardCharsets.UTF_8);InputSource inputSource = new InputSource(inputStream);XMLReader reader = getXMLReader(context);reader.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);ParseXmlExceptionHandler errorHandler = new ParseXmlExceptionHandler();reader.setErrorHandler(errorHandler);SAXSource source = new SAXSource(reader, inputSource);xmlObject = ((UnmarshallerImpl) unmarshaller).unmarshal0(source, (JaxBeanInfo) null);} catch (Exception e) {log.error(e.getMessage(), e);}return xmlObject;}public static XMLReader getXMLReader(JAXBContext context) throws JAXBException {try {SAXParserFactory parserFactory = XmlFactory.createParserFactory(((JAXBContextImpl) context).disableSecurityProcessing);parserFactory.setValidating(false);return parserFactory.newSAXParser().getXMLReader();} catch (ParserConfigurationException var2) {throw new JAXBException(var2);} catch (SAXException var3) {throw new JAXBException(var3);}}/*** @param javaBean* @param <T>* @return* @description: 将包含@XmlRootElement的jaxb javaBean转换Xml*/public static <T> String javaBeanToXmlStr(T javaBean, Boolean noHeadFlag, Boolean keepSpecCharFlag) {try {JAXBContext context = JAXBContext.newInstance(javaBean.getClass());Marshaller marshaller = context.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 去掉报文头if (noHeadFlag) {marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);}// 禁止转义if (keepSpecCharFlag) {CharacterEscapeHandler escapeHandler = NoEscapeHandler.theInstance;marshaller.setProperty("com.sun.xml.internal.bind.characterEscapeHandler", escapeHandler);}StringWriter writer = new StringWriter();marshaller.marshal(javaBean, writer);return writer.toString();} catch (Exception e) {log.error(e.getMessage(), e);}return null;}/*** @param str* @return* @description: 将xml字符串进行加密*/public static String escapeXmlStr(String str) {if (StringUtils.isNotBlank(str)) {String escapedData = StringEscapeUtils.escapeXml11(str);return escapedData;}return null;}
}
珠玑
聊点心得:
1. 关于jaxb实体类的快速生成。
可以借助jxc或者 jdk自带tools,参考博主另一篇博文
【开发心得】Java xsd文件转JavaBean-CSDN博客最近又要对接友商老的系统,依然采用http + xml方式的请求,客方提供了xsd,这里提供windows平台两种转换xsd文件为javaBean的方式。https://blog.csdn.net/qq_26834611/article/details/133788946?spm=1001.2014.3001.55012. jaxb生成xml去掉报文头
参考如下设置:
JAXBContext context = JAXBContext.newInstance(javaBean.getClass());Marshaller marshaller = context.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 去掉报文头if (noHeadFlag) {marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);}
3. 控制是否需要转义字符
jaxb控制字符转义,jaxb不转义字符。如下
// 禁止转义if (keepSpecCharFlag) {CharacterEscapeHandler escapeHandler = NoEscapeHandler.theInstance;marshaller.setProperty("com.sun.xml.internal.bind.characterEscapeHandler", escapeHandler);}StringWriter writer = new StringWriter();marshaller.marshal(javaBean, writer);return writer.toString();
有资料显示设置如下参数即可。
com.sun.xml.bind.characterEscapeHandler
但是实际上,java1.8 (331) 是
com.sun.xml.internal.bind.characterEscapeHandler
暂时没有去探究哪个版本开始变更的,遇到这种情况,直接翻一下源码即可。
它总共有4个实现类,如果不满足你的需求,可以手动实现并且替换即可。
4. 单独转义特殊字符。
实际上jaxb对于特殊字符的转义,默认不包含引号,这里使用StringEscapeUtils.escapeXml11()进行转义。
String escapedData = StringEscapeUtils.escapeXml11(str);return escapedData;
总结: 主要在实际开发中遇到了"JAXB嵌套,其中一个字段为String,存储的是xml"的情况。
这篇关于【开发心得】Jaxb使用珠玑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!