xml与java实体相互转化
2016-08-17 16:56
267 查看
1、实体转换类:
public abstract class BuildXmlUtils<T> {
/**
* 获得泛型实体
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public T getT() throws InstantiationException, IllegalAccessException {
Type sType = getClass().getGenericSuperclass();
Type[] generics = ((ParameterizedType) sType).getActualTypeArguments();
Class<T> mTClass = (Class<T>) (generics[0]);
return mTClass.newInstance();
}
/**
* 实体bean转成xml
* @param message 实体bean
* @return String
*/
public String bean2xml(T message) {
String result = null;
JAXBContext context = null;
Marshaller marshaller = null;
String __defult_encoding = "UTF-8";
try {
context = JAXBContext.newInstance(message.getClass());
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, __defult_encoding);
StringWriter writer = new StringWriter();
marshaller.marshal(message, writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* xml转实体bean
* @param xmlStr xml字符窜
* @return
*/
@SuppressWarnings("unchecked")
public T xml2bean(String xmlStr) {
T message = null;
Reader reader = null;
JAXBContext context = null;
Unmarshaller unmarshaller = null;
try {
reader = new StringReader(xmlStr);
context = JAXBContext.newInstance(getT().getClass());
unmarshaller = context.createUnmarshaller();
message = (T) unmarshaller.unmarshal(reader);
reader.close();
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
reader = null;
e.printStackTrace();
}
}
return message;
}
}
2、在实体需要使用@XmlAccessorType、@XmlType、@XmlRootElement等xml标签进行注解
public abstract class BuildXmlUtils<T> {
/**
* 获得泛型实体
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public T getT() throws InstantiationException, IllegalAccessException {
Type sType = getClass().getGenericSuperclass();
Type[] generics = ((ParameterizedType) sType).getActualTypeArguments();
Class<T> mTClass = (Class<T>) (generics[0]);
return mTClass.newInstance();
}
/**
* 实体bean转成xml
* @param message 实体bean
* @return String
*/
public String bean2xml(T message) {
String result = null;
JAXBContext context = null;
Marshaller marshaller = null;
String __defult_encoding = "UTF-8";
try {
context = JAXBContext.newInstance(message.getClass());
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, __defult_encoding);
StringWriter writer = new StringWriter();
marshaller.marshal(message, writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* xml转实体bean
* @param xmlStr xml字符窜
* @return
*/
@SuppressWarnings("unchecked")
public T xml2bean(String xmlStr) {
T message = null;
Reader reader = null;
JAXBContext context = null;
Unmarshaller unmarshaller = null;
try {
reader = new StringReader(xmlStr);
context = JAXBContext.newInstance(getT().getClass());
unmarshaller = context.createUnmarshaller();
message = (T) unmarshaller.unmarshal(reader);
reader.close();
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
reader = null;
e.printStackTrace();
}
}
return message;
}
}
2、在实体需要使用@XmlAccessorType、@XmlType、@XmlRootElement等xml标签进行注解
相关文章推荐
- 【XStream】xml和java实体的相互转化
- java用xstream实现xml与实体的互相转化
- 使用jaxb使得java对象与xml之间相互转化
- Java对象与XML文件之间相互转化(含源代码)
- 关于java实体在xml报文和json报文之间的相互转换
- xml与java对象之间的相互转化
- \t\tJAXB技术实现java对象与xml相互转化
- java对象与XML相互转化
- xml与java对象的相互转化
- XMLGregorianCalendar和java.util.Date的相互转化
- JAVA实体XML相互转换
- JAXB技术实现java对象与xml相互转化
- JAXB实现java对象与xml相互转化
- java中String和int之间的相互转化
- JAVA中IP和整数相互转化
- 用Apache Commons Betwixt2 在XML与Java对象之间相互转换
- 利用XStream在Java对象和XML之间相互转换
- 利用XStream在Java对象和XML之间相互转换
- DataTable与Xml的相互转化
- Java中int,String类型相互转化