java对象数据转xml⽂件(加![CDATA[]])[注解⽅式]
java对象数据转xml⽂件
这种操作⼀般发⽣在导出⽂件,我在这⾥记录⼀下这⼀系列注解的使⽤,还有我遇到的问题及解决⽅法;
问题:对象属性中有可能存在计算逻辑'<'或'>',⽽在xml⽂件中这两个符号是不合法的,会转换为<和>,这样数据就'坏'了,所以<![CDATA[]]>的加⼊是⾮常有必要的!
需要导⼊类
1.这是在你需要导出的实体类中⽤到的(⼀般都是DTO[扩展类])
import java.io.Serializable;
l.bind.annotation.XmlAccessType;
l.bind.annotation.XmlAccessorType;
l.bind.annotation.XmlElement;
l.bind.annotation.XmlRootElement;
l.bind.annotation.XmlType;
l.bind.annotation.adapters.XmlJavaTypeAdapter;
2.这是解决<,>两个符号⾃动转化的⼯具类⽤到的
import java.io.StringReader;
import java.io.StringWriter;
l.bind.JAXBContext;
l.bind.Marshaller;
l.bind.Unmarshaller;
import org.apachemons.logging.Log;
import org.apachemons.logging.LogFactory;
注解的使⽤
1.@XmlRootElement,⽤于类级别的注解,对应xml的跟元素。通过name属性定义这个根节点的名称。
2.@XmlAccessorType,定义映射这个类中的何种类型都需要映射到xml。(如果不存在@XmlAccessorType,默认使⽤XmlAccessType.PUBLIC_MEMBER注解)
参数:XmlAccessType.FIELD: java对象中的所有成员变量。
XmlAccessType.PROPERTY:java对象中所有通过getter/setter⽅式访问的成员变量。
XmlAccessType.PUBLIC_MEMBER:java对象中所有的public访问权限的成员变量和通过getter/setter⽅式访问的成员变量。 XmlAccessType.NONE: java对象的所有属性都不映射为xml的元素。
3.@XmlAttribute,⽤于把java对象的属性映射为xml的属性,并可通过name属性为⽣成的xml属性指定别名。
4.@XmlElement,指定⼀个字段或get/set⽅法映射到xml的节点。通过name属性定义这个根节点的名称。
5.@XmlElementWrapper,为数组或集合定义⼀个⽗节点。通过name属性定义这个⽗节点的名称。
xml,javaBeen的相互装换
public class JaxbUtil
{
/**
* ⽇志
*/
private static final Log _logger = Log (JaxbUtil.class);
public static String convertToXml (Object obj)
{
return convertToXml (obj, "UTF-8");
}
/
**
* JavaBean转换成xml
*
* @param obj
* @param encoding
* @return
*/
public static String convertToXml (Object obj, String encoding)
{
String result = null;
try
{
JAXBContext context = wInstance (Class ());
Marshaller marshaller = ateMarshaller ();
marshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty (Marshaller.JAXB_ENCODING, encoding);
StringWriter writer = new StringWriter ();
marshaller.marshal (obj, writer);
result = String ();
}
catch (Exception ex)
{
_ (ex.getMessage (), ex);
}
return result;
}
/**
* xml转换成JavaBean
*
* @param xml
* @param c
* @return
*/
@SuppressWarnings ("unchecked")
public static <T> T converyToJavaBean (String xml, Class <T> c)
{
T t = null;
try
{
JAXBContext context = wInstance (c);
Unmarshaller unmarshaller = ateUnmarshaller ();
t = (T) unmarshaller.unmarshal (new StringReader (xml));
}
catch (Exception ex)
{
_ (ex.getMessage (), ex);
}
return t;
}
}
解决保护数据问题的⼯具类
public class CDataAdapter extends XmlAdapter <String, String>
{
@Override
public String marshal (String v) throws Exception
{
return"<![CDATA[" + v + "]]>";
}
@Override
public String unmarshal (String v) throws Exception
{
if ("<![CDATA[]]>".equals (v))
{
return"";
}
String v1 = null;
String v2 = null;
String subStart = "<![CDATA[";
int a = v.indexOf (subStart);
if (a >= 0)
{
v1 = v.substring (subStart.length (), v.length ());
}
else
{
return v;
}
String subEnd = "]]>";
int b = v1.indexOf (subEnd);
if (b >= 0)
{
v2 = v1.substring (0, b);
}
return v2;
}
}
对应实例
定义类
@XmlAccessorType (XmlAccessType.FIELD)
@XmlRootElement
@XmlType (name = "indicatorDefine", propOrder =
{ "indCode", "indDescs", "indTypeCode", "monTableName", "monTableDescs", "monTableFieldName", "monTableFieldDescs", "validateType", "validateDescs", "validateScript", "validateMethod", "relateTable", "relateScript", "stabilityIdx",
"minValue", "maxValue", "enableStatus" })
public class IndicatorDefineExportDto implements Serializable
{
private static final long serialVersionUID = 4903249232957489027L;
@XmlElement (name = "indCode")
private String indCode;
@XmlElement (name = "indDescs")
private String indDescs;
@XmlElement (name = "indTypeCode")
private String indTypeCode;
@XmlElement (name = "monTableName")
private String monTableName;
@XmlElement (name = "monTableDescs")
private String monTableDescs;
@XmlElement (name = "monTableFieldName")
private String monTableFieldName;
@XmlElement (name = "monTableFieldDescs")
private String monTableFieldDescs;
使⽤
private String monTableFieldDescs;
@XmlElement (name = "validateType")
private String validateType;
@XmlElement (name = "validateDescs")
private String validateDescs;
@XmlJavaTypeAdapter (CDataAdapter.class)//这⾥是该属性需要加<![CDATA[]]>防⽌转换的注解
@XmlElement (name = "validateScript")
private String validateScript;
@XmlElement (name = "validateMethod")
private String validateMethod;
@XmlJavaTypeAdapter (CDataAdapter.class)//这⾥是该属性需要加<![CDATA[]]>防⽌转换的注解
@XmlElement (name = "relateTable")
private String relateTable;
@XmlElement (name = "relateScript")
private String relateScript;
@XmlElement (name = "stabilityIdx")
private String stabilityIdx;
@XmlElement (name = "minValue")
private String minValue;
@XmlElement (name = "maxValue")
private String maxValue;
@XmlElement (name = "enableStatus")
private String enableStatus;
//get,set⽅法省略
}
@XmlAccessorType (XmlAccessType.FIELD)
@XmlRootElement (name = "IndicatorDefineData")
@XmlType (propOrder =
java xml是什么{ "indicatorDefines" })
public class IndicatorDefines implements Serializable
{
/
**
* serialVersionUID:TODO
*
* @since Ver 1.1
*/
private static final long serialVersionUID = 3140176739363502638L;
@XmlElementWrapper (name = "indicatorDefines")
@XmlElement (name = "indicatorDefine")
private List <IndicatorDefineExportDto> indicatorDefines;
List <IndicatorDefineExportDto> indicatorDefineExportDtos = new ArrayList <IndicatorDefineExportDto> (); IndicatorDefineExportDto indicatorDefineExportDto = new IndicatorDefineExportDto ();
//省略查询数据操作
//indicatorDefineInfos--查询到的数据
for (IndicatorDefine indicatorDefine : indicatorDefineInfos){
IndicatorDefineExportDto indicatorDefineExportDto = new IndicatorDefineExportDto ();
//省略...
//将indicatorDefine数据赋值到indicatorDefineExportDto 属性上
indicatorDefineExportDtos.add (indicatorDefineExportDto);
}
indicatorDefines.setIndicatorDefines (indicatorDefineExportDtos);
//⽣成xml内容
String xmlContent = vertToXml (indicatorDefines);
/
/以下省略...就是将⽂件传到前台
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论