The XStream tool is typically used. Converts a collection, an array, an object into XML.
Import Two packages:
Xpp3_min-1.1.4c.jar
Xstream-1.4.4.jar
Customizing a Class
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;} @Overridepublic String toString () {return "User [id=" + ID + ", name=" + name + "]";}}
Test class
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 (one); U.setname ("Wind Qing"); User U1=new User (); U1.setid (n); U1.setname ("Young Sister"); Arraylist<user> array=new arraylist<user> (); Array.add (U); Array.add (U1); //Create XStream Object XStream xs=new XStream ();
Converts an object using the XStream object string xml = Xs.toxml (array);
//Output Conversion of SYSTEM.OUT.PRINTLN (XML);}}
Output Result:
<list> <com.baidu.user>//represents the properties of the class's full path <id>11</id>//class <name> Wind </ Properties of the name>//class </com.baidu.User> <com.baidu.User> <id>12</id> <name > younger sister </name> </com.baidu.User></list>
Make the property label a property of the class label (contained within)
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 (one); U.setname ("Wind Qing"); User U1=new User (); U1.setid (n); U1.setname ("Young Sister"); Arraylist<user> array=new arraylist<user> (); Array.add (U); Array.add (U1);//Convert collection to Xmlxstream xs=new XStream (); //Xs.useattributefor The property in the class as the inner property of the label of the Class (User.class, "id"); xs.useattributefor (User.class, "name"); String XML = xs.toxml (array); SYSTEM.OUT.PRINTLN (XML);}}
Output Result:
<list> <com.baidu.user id= "One" name= "Wind Qing"/> <com.baidu.user id= "" Name= "Xiao Sister"/></ List>
Modify the full path of the class to the class 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 (one); U.setname ("Wind Qing"); User U1=new User (); U1.setid (n); U1.setname ("Young Sister"); Arraylist<user> array=new arraylist<user> (); Array.add (U); Array.add (U1);//Convert collection to Xmlxstream 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);}}
Output Result:
<list> <user id= "One" name= "Wind Qing"/> <user id= "" Name= "Xiao Sister"/></list>
Using jquery to parse XML (DOM)
' $.post ("${pagecontext.request.contextpath}/getcityservlet", {"pid":p v},function (data) {$ ("#city"). HTML (" <option>-Please select-</option> "); $ (data). Find ("Cities"). each (function () {
/*
<Cities> <id>18</id> <cityid>140300</cityid> <city> Yangquan </ City> <provinceid>140000</provinceid>
*///alert (this). html ()), $ ("#city"). Append ("<option value= '" +$ (This). Find ("Cityid"). Text () + ">" + $ (this). Find ("City"). Text () + "</option>");});
$.post ("${pagecontext.request.contextpath}/getcityservlet", {"pid":p v},function (data) {$ ("#city"). HTML ("< option>-Please select-</option> "), $ (data). Find (" Cities "). each (function () {/* <cities cityid=" 131100 "city=" Hengshui " > <id>15</id> <provinceid>130000</provinceid> </Cities> *///alert ( $ (this). attr ("City"), $ ("#city"). Append ("<option value=" "+$ (This). attr (" Cityid ") +" > "+ $ (This). attr (" City " ) + "</option>");}) ; });
Day29 (object to XML (using Java))