XML与对象互转
来源:互联网 发布:linux ftp 断点续传 编辑:程序博客网 时间:2024/06/05 19:46
使用jaxb xml与对象javabean之间转换,对象生成soap报文xml,soap报文xml转换为对象
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.lang.StringUtils;
/**
* 使用Jaxb2.0实现XML<->Java Object的Binder.
*
* 特别支持Root对象是List的情形.
*
*/
@SuppressWarnings("rawtypes")
public class JaxbBinder {
//多线程安全的Context.
private JAXBContext jaxbContext;
/**
* @param types 所有需要序列化的Root对象的类型.
*/
public JaxbBinder(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的情形.
*/
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);
}
}
/**
* 创建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 {
@XmlAnyElement
protected Collection collection;
}
}
原方法
@SuppressWarnings("unchecked")
public <T> T fromXML(String fileName) {
return (T)fromXML(new File(fileName));
}
@SuppressWarnings("unchecked")
public <T> T fromXML(File file) {
try {
Unmarshaller unmarshaller = createUnmarshaller();
return (T) unmarshaller.unmarshal(file);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public <T> T fromXML(InputStream stream) {
try {
Unmarshaller unmarshaller = createUnmarshaller();
return (T) unmarshaller.unmarshal(stream);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
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.lang.StringUtils;
/**
* 使用Jaxb2.0实现XML<->Java Object的Binder.
*
* 特别支持Root对象是List的情形.
*
*/
@SuppressWarnings("rawtypes")
public class JaxbBinder {
//多线程安全的Context.
private JAXBContext jaxbContext;
/**
* @param types 所有需要序列化的Root对象的类型.
*/
public JaxbBinder(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的情形.
*/
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);
}
}
/**
* 创建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 {
@XmlAnyElement
protected Collection collection;
}
}
原方法
@SuppressWarnings("unchecked")
public <T> T fromXML(String fileName) {
return (T)fromXML(new File(fileName));
}
@SuppressWarnings("unchecked")
public <T> T fromXML(File file) {
try {
Unmarshaller unmarshaller = createUnmarshaller();
return (T) unmarshaller.unmarshal(file);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public <T> T fromXML(InputStream stream) {
try {
Unmarshaller unmarshaller = createUnmarshaller();
return (T) unmarshaller.unmarshal(stream);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
0 0
- XML与对象互转
- JAVA对象与XML的互转
- Java对象与xml文件互转
- xml与java对象互转
- JAXB:xml与java对象互转
- JAXB完成XML与Java对象的互转
- JAXB完成XML与Java对象的互转
- JAXB完成XML与Java对象的互转
- XStream java xml 与 java对象互转
- XML与java对象互转文档
- xStream完美转换XML、JSON /XStream Xml与JavaBean对象的互转经典【转载】
- 对象与XML的互换
- java对象与xml文件
- xml与对象的转换
- xml与java对象映射
- 使用JAXB时,xml与java对象互转以及初始情况下直接由模板xml生成javabean
- Java JSON、XML文件/字符串与Bean对象互转解析
- 使用JAXB类库实现XML与JAVA对象的互转
- androoid framework学习之activity组件
- [Gradle中文教程系列]-跟我学Gradle-5.2:依赖的分类
- swift switch tuple where
- c语言编写扫雷
- libjingle的重要概念之一:Signals
- XML与对象互转
- java中的线程池实现以及代码分析
- java gradle
- Android 5.1 录音流程
- OpenCV SIFT/SURF图片拼接融合
- Android中Activity的使用,简单实例讲解
- Android案例算法——拼图是否有解算法
- java 拼接字符串时,去掉最后一个多余的逗号
- 魔兽争霸3冰封皇座 不死族Undead英雄使用经验和建筑顺序