xstream如何将java对象转化成有cdata的xml
答案:3 悬赏:30 手机版
解决时间 2021-01-29 05:45
- 提问者网友:酱爆肉
- 2021-01-28 09:51
xstream如何将java对象转化成有cdata的xml
最佳答案
- 五星知识达人网友:雪起风沙痕
- 2021-01-28 10:27
实体类要加注解
实体类要加注解
@XStreamAlias("msg")
public class Msg implements Serializable {
private static final long serialVersionUID = 3196422897798539811L;
@XStreamAsAttribute
private String toUid;
@XStreamAsAttribute
private String fromUid;
@XStreamAsAttribute
private String flag;
@XStreamAsAttribute
private String time;
@XStreamAlias("content")
private String content;
@XStreamAlias("user")
private String user;
//省略geter、seter
}
然后写个工具类
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class SerializeXmlUtil {
public static String serializeToXml(Object obj) {
//XStream xstream = new XStream();
//直接用jaxp dom来解释
//XStream xstream=new XStream(new DomDriver());
//指定编码解析器,直接用jaxp dom来解释
XStream xstream=new XStream(new DomDriver("utf-8"));
//如果没有这句,xml中的根元素会是<包.类名>
//或者说:注解根本就没生效,所以的元素名就是类的属性
xstream.processAnnotations(obj.getClass());
StringBuffer xmlstrbuf = new StringBuffer();
xmlstrbuf.append("");
xmlstrbuf.append("
");
xmlstrbuf.append(xstream.toXML(obj));
return xmlstrbuf.toString();
}
public static T deSerizalizeFromXml(String xmlstr, Class cls) throws Exception {
//注意:不是new Xstream();否则报错:java.lang.NoClassDefFoundError:
//org/xmlpull/v1/XmlPullParserFactory
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(cls);
T obj = (T) xstream.fromXML(xmlstr);
return obj;
}
}
实体类要加注解
@XStreamAlias("msg")
public class Msg implements Serializable {
private static final long serialVersionUID = 3196422897798539811L;
@XStreamAsAttribute
private String toUid;
@XStreamAsAttribute
private String fromUid;
@XStreamAsAttribute
private String flag;
@XStreamAsAttribute
private String time;
@XStreamAlias("content")
private String content;
@XStreamAlias("user")
private String user;
//省略geter、seter
}
然后写个工具类
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class SerializeXmlUtil {
public static String serializeToXml(Object obj) {
//XStream xstream = new XStream();
//直接用jaxp dom来解释
//XStream xstream=new XStream(new DomDriver());
//指定编码解析器,直接用jaxp dom来解释
XStream xstream=new XStream(new DomDriver("utf-8"));
//如果没有这句,xml中的根元素会是<包.类名>
//或者说:注解根本就没生效,所以的元素名就是类的属性
xstream.processAnnotations(obj.getClass());
StringBuffer xmlstrbuf = new StringBuffer();
xmlstrbuf.append("");
xmlstrbuf.append("
");
xmlstrbuf.append(xstream.toXML(obj));
return xmlstrbuf.toString();
}
public static
//注意:不是new Xstream();否则报错:java.lang.NoClassDefFoundError:
//org/xmlpull/v1/XmlPullParserFactory
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(cls);
T obj = (T) xstream.fromXML(xmlstr);
return obj;
}
}
全部回答
- 1楼网友:三千妖杀
- 2021-01-28 11:48
网上很多。百度。
- 2楼网友:不甚了了
- 2021-01-28 11:23
private static XStream xstream = new XStream(new XppDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
// 对所有xml节点的转换都增加CDATA标记
boolean cdata = true;
@SuppressWarnings("unchecked")
public void startNode(String name, Class clazz) {
super.startNode(name, clazz);
}
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write(" writer.write(text);
writer.write("]]>");
} else {
writer.write(text);
}
}
};
}
});
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
// 对所有xml节点的转换都增加CDATA标记
boolean cdata = true;
@SuppressWarnings("unchecked")
public void startNode(String name, Class clazz) {
super.startNode(name, clazz);
}
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write(" writer.write(text);
writer.write("]]>");
} else {
writer.write(text);
}
}
};
}
});
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯