jaxb工具类
2016-02-24 13:58
225 查看
import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; @SuppressWarnings({ "unchecked" }) public class XmlUtil<T> { public static String toXml(Object req, Class<?> c) { try { StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(c); javax.xml.bind.Marshaller marshaller = context.createMarshaller(); marshaller.marshal(req, new StreamResult(writer)); String result = writer.toString(); writer.close(); return result; } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } public static <T> T toObj(String respXml, Class<T> c) { try { JAXBContext context = JAXBContext.newInstance(c); javax.xml.bind.Unmarshaller unmarshaller = context.createUnmarshaller(); StringReader reader = new StringReader(respXml); T result = (T) unmarshaller.unmarshal(new StreamSource(reader)); reader.close(); return result; } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } }
相关文章推荐
- java使用jaxb操作xml示例
- JavaXML
- JAXB XML与对象之间转换
- JAXB
- java jaxb xml
- JAXB 解决XML与JavaBean的互换
- JAXB “Class has two properties of the same name” issue
- 采用JAX-WS Annotated Services from Java+JAXB发布获取web service
- cxf 异常
- JAXB技术的魔力
- JDK1.6新特性
- jaxb和tomcat问题
- JAXB实现XML和Java对象绑定
- Java Web服务指南
- 关于使用jaxb做配置解析
- activeMQ启动错误,引出的java endorsed目录问题。
- jaxb 简介
- jaxb 简介 2
- axis1.4--沉淀于webservice河床里
- JAXB