| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package com.happy.Until.weixin;
- import org.apache.commons.lang.StringUtils;
- import javax.xml.bind.*;
- import javax.xml.bind.annotation.XmlAnyElement;
- import javax.xml.namespace.QName;
- import java.io.StringReader;
- import java.io.StringWriter;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- /**
- * 使用Jaxb2.0实现XML<->Java Object的Binder.
- *
- * 特别支持Root对象是List的情形.
- *
- * @author
- */
- public class JaxbUtil {
- // 多线程安全的Context.
- private JAXBContext jaxbContext;
-
- /**
- * @param types
- * 所有需要序列化的Root对象的类型.
- */
- public JaxbUtil(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, 特别支持对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, 支持大小写敏感或不敏感.
- */
- @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);
- }
- }
-
- /**
- * 创建Marshaller, 设定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);
- }
- }
-
- /**
- * 创建UnMarshaller.
- */
- public Unmarshaller createUnmarshaller() {
- try {
- return jaxbContext.createUnmarshaller();
- } catch (JAXBException e) {
- throw new RuntimeException(e);
- }
- }
-
- /**
- * 封装Root Element 是 Collection的情况.
- */
- public static class CollectionWrapper {
- @SuppressWarnings("unchecked")
- @XmlAnyElement
- protected Collection collection;
- }
- /**
- * 把map转换成xml格式
- * @param params
- * @return
- * @throws Exception
- */
- public static String getRequestXml(Map<String, String> params) throws Exception{
- StringBuffer sb = new StringBuffer();
- sb.append("<xml>");
- Set<Entry<String, String>> es = params.entrySet();
- Iterator<Entry<String, String>> it = es.iterator();
- while (it.hasNext()) {
- Entry<String, String> entry = (Entry<String, String>) it
- .next();
- String k = (String) entry.getKey();
- String v = (String) entry.getValue();
- if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k)) {
- sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
- } else {
- sb.append("<" + k + ">" + v + "</" + k + ">");
- }
- }
- sb.append("</xml>");
- return new String(sb.toString().toString().getBytes(), "utf-8");
- }
- }
|