Android XMl parsing magic xstream 6: converting the set list into XML documents, androidxstream
For more information about xstream, see:
Android XMl parsing magic xstream 1: parse the aa. xml file in the asset folder of the android Project
Android XMl parsing magic xstream 2: Converting objects into xml
Android XMl parsing magic xstream 3: Converting complex objects into xml
Android XMl parsing magic xstream 4: parsing complex xml files as objects
Android XMl parsing magic xstream 5: Convert complex objects into xml and write them into xml files on the SD card
1. Create a JavaBeen
package com.android10;public class Person { String pName ; String pAge ; public String getpName() { return pName; } public void setpName(String pName) { this.pName = pName; } public String getpAge() { return pAge; } public void setpAge(String pAge) { this.pAge = pAge; } }
package com.android10;public class Product { private String name ; private String age ; private Person person ; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; }}
package com.android10;import java.util.List;public class ListBean { private List<Product> root ; public List<Product> getRoot() { return root; } public void setRoot(List<Product> root) { this.root = root; }}
2. Main Methods
Package com. android10; import java. io. byteArrayInputStream; import java. io. byteArrayOutputStream; import java. util. arrayList; import java. util. list; import javax. xml. transform. outputKeys; import javax. xml. transform. source; import javax. xml. transform. transformer; import javax. xml. transform. sax. SAXSource; import javax. xml. transform. sax. SAXTransformerFactory; import javax. xml. transform. stream. streamResult; import org. xml. sax. inputSource; import android. app. activity; import android. OS. bundle; import com. thoughtworks. xstream. XStream; public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); XStream xstream = new XStream (); List <Product> root = getList (); // set the empty element of the Set in ListBean, that is, the set element tag xstream is not displayed. addImplicitCollection (ListBean. class, "root"); xstream. autodetectAnnotations (true); // sets the alias xstream. alias ("product", Product. class); // set name as the attribute of the element of the parent class (Student) xstream. useAttributeFor (Product. class, "name"); // converts the list set to an Xml String xmlString = xstream. toXML (root); // write the Xml string to the Xml file XstreamUtil xstreamUtil = new XstreamUtil (); xstreamUtil. writeToXml (this, xmlString); // converts an Xml string to a list set List <Product> list = new ArrayList <Product> (); list = (List <Product>) xstream. fromXML (xmlString); System. out. println ("sss" + formatXml (xmlString);}/*** get data * @ return */private List <Product> getList () {Person person1 = new Person (); person1.setpName ("saliy"); person1.setpAge ("36"); Product product1 = new Product (); product1.setName ("jhon "); product1.setAge ("30"); product1.setPerson (person1); Person person2 = new Person (); person2.setpName ("saliy02"); person2.setpAge ("3602 "); product product2 = new Product (); product2.setName ("jhon02"); product2.setAge ("3002"); product2.setPerson (person2 ); list <Product> root = new ArrayList <Product> (); root. add (product1); root. add (product2); return root;}/*** format the XML String * @ param xml * @ return */public static String formatXml (String xml) {try {Transformer serializer = SAXTransformerFactory. newInstance (). newTransformer (); serializer. setOutputProperty (OutputKeys. INDENT, "yes"); serializer. setOutputProperty ("{http://xml.apache.org/javast=indent-amount", "2"); Source xmlSource = new SAXSource (new InputSource (new ByteArrayInputStream (xml. getBytes (); StreamResult res = new StreamResult (new ByteArrayOutputStream (); serializer. transform (xmlSource, res); return new String (ByteArrayOutputStream) res. getOutputStream ()). toByteArray ();} catch (Exception e) {return xml ;}}}
Package com. android10; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStreamWriter; import android. content. context; import android. OS. environment; public class XstreamUtil {XcallBack xcallBack;/*** write the xml string to the SD card file * @ param context * @ param str xml string */public void writeToXml (Context context, string str) {// get File path String SDPATH = Environment. getExternalStorageDirectory () + "/myfile1.xml/"; // create a file File = new file (SDPATH); if (! File. exists () {try {file. createNewFile ();} catch (IOException e) {e. printStackTrace () ;}// write data try {FileOutputStream out = new FileOutputStream (file); OutputStreamWriter outw = new OutputStreamWriter (out); try {outw. write (str); outw. close (); out. close (); if (xcallBack! = Null) {xcallBack. success () ;}} catch (IOException e) {if (xcallBack! = Null) {xcallBack. fail () ;}} catch (FileNotFoundException e1) {e1.printStackTrace (); if (xcallBack! = Null) {xcallBack. fail () ;}}// set the listener void setXStreamLister (XcallBack xcallBack) {this. xcallBack = xcallBack;} interface XcallBack {/*** Write Succeeded */void success ();/*** write failed */void fail ();}
3. Running result
<List>
<Product name = "jhon">
<Age> 30 </age>
<Person>
<PAge> 36 </pAge>
<PName> saliy </pName>
</Person>
</Product>
<Product name = "jhon02">
<Age> 3002 </age>
<Person>
<PAge> 3602 </pAge>
<PName> saliy02 </pName>
</Person>
</Product>
</List>