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() {
- @Override
- public 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,参考博主另一篇博文
参考如下设置:
- 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"的情况。