国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Jaxb解析器

2019-11-09 19:32:43
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
package com.mx.tmc.core.util;import java.io.StringReader;import java.io.StringWriter;import java.util.Collection;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBElement;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;import javax.xml.bind.annotation.XmlAnyElement;import javax.xml.namespace.QName;import org.apache.commons.lang3.StringUtils;public class JAXBParser {// 多線程安全的Context.PRivate JAXBContext jaxbContext;/*** @param types*            所有需要序列化的Root對(duì)象的類型.*/public JAXBParser(Class<?> types) {try {jaxbContext = JAXBContext.newInstance(types);} catch (JAXBException e) {throw new RuntimeException(e);}}/*** Java Object->Xml.*/public String toXml(Object root, String encoding) {try {StringWriter writer = new StringWriter();createMarshaller(encoding).marshal(root, writer);return writer.toString();} catch (JAXBException e) {throw new RuntimeException(e);}}/*** Java Object->Xml, 特別支持對(duì)Root Element是Collection的情形.*/@SuppressWarnings("unchecked")public String toXml(Collection root, String rootName, String encoding) {try {CollectionWrapper wrapper = new CollectionWrapper();wrapper.collection = root;JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),CollectionWrapper.class, wrapper);StringWriter writer = new StringWriter();createMarshaller(encoding).marshal(wrapperElement, writer);return writer.toString();} catch (JAXBException e) {throw new RuntimeException(e);}}/*** Xml->Java Object.*/@SuppressWarnings("unchecked")public <T> T fromXml(String xml) {try {StringReader reader = new StringReader(xml);return (T) createUnmarshaller().unmarshal(reader);} catch (JAXBException e) {throw new RuntimeException(e);}}/*** Xml->Java Object, 支持大小寫(xiě)敏感或不敏感.*/@SuppressWarnings("unchecked")public <T> T fromXml(String xml, boolean caseSensitive) {try {String fromXml = xml;if (!caseSensitive)fromXml = xml.toLowerCase();StringReader reader = new StringReader(fromXml);return (T) createUnmarshaller().unmarshal(reader);} catch (JAXBException e) {throw new RuntimeException(e);}}/*** 創(chuàng)建Marshaller, 設(shè)定encoding(可為Null).*/public Marshaller createMarshaller(String encoding) {try {Marshaller marshaller = jaxbContext.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);if (StringUtils.isNotBlank(encoding)) {marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);}return marshaller;} catch (JAXBException e) {throw new RuntimeException(e);}}/*** 創(chuàng)建UnMarshaller.*/public Unmarshaller createUnmarshaller() {try {return jaxbContext.createUnmarshaller();} catch (JAXBException e) {throw new RuntimeException(e);}}/*** 封裝Root Element 是 Collection的情況.*/public static class CollectionWrapper {@SuppressWarnings("unchecked")@XmlAnyElementprotected Collection collection;}}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 清原| 蓝山县| 绵竹市| 奎屯市| 大田县| 宁阳县| 永州市| 红河县| 许昌市| 南岸区| 澄城县| 行唐县| 通道| 英德市| 中牟县| 巴楚县| 潮安县| 扎鲁特旗| 庆城县| 崇阳县| 科技| 增城市| 彝良县| 五大连池市| 武胜县| 化德县| 仙游县| 石首市| 弥勒县| 固始县| 宁陵县| 泰安市| 二连浩特市| 天等县| 泰顺县| 习水县| 小金县| 衡阳市| 黎川县| 万安县| 洪湖市|