用 Jaxb 对 XML 和 JavaBean相互转换

用ConcurrentMap 对JAXBContext进行缓存,提高效率。


private static ConcurrentMap<Class<?>, JAXBContext> jaxbContexts = new ConcurrentHashMap<Class<?>, JAXBContext>();

	/**
	 * 把xml转换成对应的bean
	 * 
	 * @param <T>
	 * @param clazz
	 * @param xml
	 * @return
	 */
	public static <T> T parseXml2Bean(Class<T> clazz, String xml) {
		if (StringUtils.isBlank(xml)) {
			return null;
		}
		try {
			StringReader reader = new StringReader(xml);
			return clazz.cast(createUnmarshaller(clazz).unmarshal(reader));
		} catch (JAXBException e) {
			throw new RuntimeException("Could not parse xml to class [" + clazz + "]: " + e.getMessage(), e);
		}
	}

	/**
	 * 把bean转换成对应的xml
	 * 
	 * @param obj
	 * @return
	 */
	public static String parseBean2Xml(Object obj) {
		return parseBean2Xml(obj, null, false);
	}

	/**
	 * 是否省略xml头信息
	 * 
	 * @param obj
	 * @param encoding
	 *            编码格式
	 * @param bool
	 *            是否省略xml头信息
	 * @return
	 */
	public static String parseBean2Xml(Object obj, String encoding, boolean bool) {
		if (obj == null) {
			return null;
		}
		try {
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			createMarshaller(obj.getClass(), encoding, bool).marshal(obj, os);
			return os.toString("UTF-8");
		} catch (JAXBException e) {
			throw new RuntimeException("Could not parse [" + obj + "] to xml " + e.getMessage(), e);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * 创建Marshaller并设定encoding(可为null). <br/>
	 * 线程不安全,需要每次创建或pooling。
	 */
	private static Marshaller createMarshaller(Class<?> clazz, String encoding, boolean bool) {
		try {
			JAXBContext jaxbContext = getJaxbContext(clazz);
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);// 是否格式化生成的xml串
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, bool);// 是否省略xml头信息
			if (StringUtils.isNotBlank(encoding)) {
				marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
			}
			return marshaller;
		} catch (JAXBException e) {
			throw new RuntimeException("Could not createMarshaller for class [" + clazz + "]: " + e.getMessage(), e);
		}
	}

	/**
	 * 创建UnMarshaller. <br/>
	 * 线程不安全,需要每次创建或pooling。
	 */
	private static Unmarshaller createUnmarshaller(Class<?> clazz) {
		try {
			JAXBContext jaxbContext = getJaxbContext(clazz);
			return jaxbContext.createUnmarshaller();
		} catch (JAXBException e) {
			throw new RuntimeException("Could not createUnmarshaller for class [" + clazz + "]: " + e.getMessage(), e);
		}
	}

	/**
	 * 维护jaxbContexts
	 */
	private static JAXBContext getJaxbContext(Class<?> clazz) {
		Validate.notNull(clazz, "'clazz' must not be null");
		JAXBContext jaxbContext = jaxbContexts.get(clazz);
		try {
			if (jaxbContext == null) {
				jaxbContext = JAXBContext.newInstance(clazz);
				JAXBContext j = jaxbContexts.putIfAbsent(clazz, jaxbContext);
				if (j != null) {
					jaxbContext = j;
				}
			}
		} catch (JAXBException e) {
			throw new RuntimeException(
					"Could not instantiate JAXBContext for class [" + clazz + "]: " + e.getMessage(), e);
		}
		return jaxbContext;
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值