标签:this xstream sys cti 属性 解析xml div 对象 attr
通常使用xStream工具. 将集合,数组,对象转成XML.
导入两个包:
xpp3_min-1.1.4c.jar
xstream-1.4.4.jar
自定义一个类
package com.baidu; public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
测试类
package com.baidu; import java.util.ArrayList; import com.thoughtworks.xstream.XStream; public class Demo { public static void main(String[] args) { User u=new User(); u.setId(11); u.setName("风清扬"); User u1=new User(); u1.setId(12); u1.setName("小师妹"); ArrayList<User> array=new ArrayList<User>(); array.add(u); array.add(u1); //创建XStream对象 XStream xs=new XStream();
//使用XStream对象转换对象 String xml = xs.toXML(array);
//输出转换的 System.out.println(xml); } }
输出结果:
<list> <com.baidu.User>//表示类的全路径 <id>11</id>//类的属性 <name>风清扬</name>//类的属性 </com.baidu.User> <com.baidu.User> <id>12</id> <name>小师妹</name> </com.baidu.User> </list>
让属性标签变为类标签的属性(内包含)
package com.baidu;
import java.util.ArrayList;
import com.thoughtworks.xstream.XStream;
public class Demo {
public static void main(String[] args) {
User u=new User();
u.setId(11);
u.setName("风清扬");
User u1=new User();
u1.setId(12);
u1.setName("小师妹");
ArrayList<User> array=new ArrayList<User>();
array.add(u);
array.add(u1);
//将集合转换为xml
XStream xs=new XStream();
//将类中的属性作为类的标签的内部属性
xs.useAttributeFor(User.class, "id");
xs.useAttributeFor(User.class,"name");
String xml = xs.toXML(array);
System.out.println(xml);
}
}
输出结果:
<list> <com.baidu.User id="11" name="风清扬"/> <com.baidu.User id="12" name="小师妹"/> </list>
修改类的全路径为类名
package com.baidu;
import java.util.ArrayList;
import com.thoughtworks.xstream.XStream;
public class Demo {
public static void main(String[] args) {
User u=new User();
u.setId(11);
u.setName("风清扬");
User u1=new User();
u1.setId(12);
u1.setName("小师妹");
ArrayList<User> array=new ArrayList<User>();
array.add(u);
array.add(u1);
//将集合转换为xml
XStream xs=new XStream();
xs.useAttributeFor(User.class, "id");
xs.useAttributeFor(User.class,"name");
xs.alias("User", User.class);
String xml = xs.toXML(array);
System.out.println(xml);
}
}
输出结果:
<list> <User id="11" name="风清扬"/> <User id="12" name="小师妹"/> </list>
使用jquery来解析xml(dom)
` $.post("${pageContext.request.contextPath}/GetCityServlet", {"pid":pv},function(data) { $("#city").html("<option>-请选择-</option>"); $(data).find("Cities").each(function(){
/*
<Cities> <id>18</id> <cityid>140300</cityid> <city>阳泉市</city> <provinceid>140000</provinceid> </Cities>
*/ //alert($(this).html()); $("#city").append("<option value=‘"+$(this).find("cityid").text()+"‘>"+ $(this).find("city").text() + "</option>"); }); });
$.post("${pageContext.request.contextPath}/GetCityServlet", {"pid":pv},function(data) { $("#city").html("<option>-请选择-</option>"); $(data).find("Cities").each(function(){ /* <Cities cityid="131100" city="衡水市"> <id>15</id> <provinceid>130000</provinceid> </Cities> */ //alert($(this).attr("city")); $("#city").append("<option value=‘"+$(this).attr("cityid")+"‘>"+ $(this).attr("city") + "</option>"); }); }); });
标签:this xstream sys cti 属性 解析xml div 对象 attr
原文地址:http://www.cnblogs.com/fjkgrbk/p/XStream.html