Generate XML:
PackageCom.itheima.createxml;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.util.ArrayList;Importjava.util.List;ImportCom.itheima.createxml.domain.Message;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View; Public classMainactivityextendsActivity {List<Message>smslist; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Virtual 10 text messageSmslist =NewArraylist<message>(); for(inti = 0; I < 10; i++) {Message SMS=NewMessage ("Little chi Good" + I, System.currenttimemillis () + "", "138" +i+i, "1"); Smslist.add (SMS); } } Public voidClick (View v) {//in the memory of the XML backup text message format splicing out, SMS saved in the external, because the app deleted after the text message is still in. StringBuffer SB =NewStringBuffer (); Sb.append ("<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?> '); Sb.append ("<messages>"); for(Message sms:smslist) {//stitching each SMS SMSSb.append ("<sms>"); Sb.append ("<body>"); Sb.append (Sms.getbody ()); Sb.append ("</body>"); Sb.append ("<date>"); Sb.append (Sms.getdate ()); Sb.append ("</date>"); Sb.append ("<type>"); Sb.append (Sms.gettype ()); Sb.append ("</type>"); Sb.append ("<address>"); Sb.append (Sms.getaddress ()); Sb.append ("</address>"); Sb.append ("</sms>"); } sb.append ("</messages>"); File File=NewFile ("Sdcard/sms.xml"); Try{FileOutputStream fos=Newfileoutputstream (file); Fos.write (Sb.tostring (). GetBytes ()); Fos.close (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Generate XML:
PackageCom.itheima.xmlserializer;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.xmlpull.v1.XmlSerializer;ImportCom.itheima.createxml.domain.Message;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.util.Xml;ImportAndroid.view.Menu;ImportAndroid.view.View; Public classMainactivityextendsActivity {List<Message>smslist; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Virtual 10 text messageSmslist =NewArraylist<message>(); for(inti = 0; I < 10; i++) {Message SMS=NewMessage ("Little chi Good" +I, System.currenttimemillis ()+ "", "138" + i + I, "1"); Smslist.add (SMS); } } Public voidClick (View v) {//To generate an XML file using an XML serializer//1. Get the Serializer objectXmlSerializer xs =Xml.newserializer (); //2. Initialize the path and file name, generated in the SD card,File File =NewFile ("Sdcard/sms2.xml"); Try{FileOutputStream fos=Newfileoutputstream (file); //enconding: Specifies what encoding to generate XML files, android default is Utf-8, so almost all of the code is UTF-8,Xs.setoutput (FOS, "Utf-8"); //utf-8 Specifies the value of enconding in the head node <message> enconding= "Utf-8", true to specify whether it is a standalone XML file,Xs.startdocument ("Utf-8",true); Xs.starttag (NULL, "message");//the name of the start node is <message>,null namespace, for(Message sms:smslist) {Xs.starttag (NULL, "SMS"); Xs.starttag (NULL, "Body"); Xs.text (Sms.getbody ()+ "<body>");//text represents a textual nodeXs.endtag (NULL, "Body"); Xs.starttag (NULL, "Date"); Xs.text (Sms.getdate ()); Xs.endtag (NULL, "Date"); Xs.starttag (NULL, "type"); Xs.text (Sms.gettype ()); Xs.endtag (NULL, "type"); Xs.starttag (NULL, "Address"); Xs.text (Sms.getaddress ()); Xs.endtag (NULL, "Address"); Xs.endtag (NULL, "SMS"); } Xs.endtag (NULL, "message");//End Node//tell the serializer that the file has been generatedxs.enddocument (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Parsing xml:
PackageCom.itheima.pullparser;ImportJava.io.InputStream;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.xmlpull.v1.XmlPullParser;Importorg.xmlpull.v1.XmlPullParserException;Importcom.itheima.pullparser.domain.City;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.util.Xml;ImportAndroid.view.Menu;ImportAndroid.view.View; Public classMainactivityextendsActivity {List<City>CityList; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Public voidClick (View v) {//get the resource file to the SRC folderInputStream is = getClassLoader (). getResourceAsStream ("Weather.xml");//get the resource as a stream//get the Pull parser objectXmlpullparser XP =Xml.newpullparser (); //Initialize Try{xp.setinput (IS,"GBK");//The first is the input stream, GBK is the parsing encoding, GBK is the default encoding format for Windows, so the Weather.xml encoding format is GBK//gets the event type of the current node, with the event type's judgment, we can know what node the current node is, thus determining what we should do//parsing is the parsing of a row of rows, intType =Xp.geteventtype (); City City=NULL; while(Type! = xmlpullparser.end_document) {//End of document node//depending on the type of node, different operations are done Switch(type) { CaseXmlpullparser.start_tag://Start Node//gets the name of the current node if("Weather". Equals (Xp.getname ())) { //Create a City collection object to hold the city's JavaBeanCityList =NewArraylist<city>(); } Else if("City". Equals (Xp.getname ())) { //Create a JavaBean object for CityCity =NewCity (); } Else if("Name". Equals (Xp.getname ())) {String name= Xp.nexttext ();//The text that gets the next node of the current node is qq,nexttext and moves the pointer to the end node of the current node.city.setname (name); } Else if("Temp". Equals (Xp.getname ())) { //gets the text of the next node of the current nodeString temp =Xp.nexttext (); City.settemp (temp); } Else if("PM". Equals (Xp.getname ())) { //gets the text of the next node of the current nodeString pm =Xp.nexttext (); CITY.SETPM (PM); } Break; CaseXmlpullparser.end_tag://End Node,<name>qq</name> encountered </name> do not do anything, encountered QQ text node do not do anything, if("City". Equals (Xp.getname ())) { //put the city's JavaBean in the collectionCitylist.add (city); } Break; } //move the pointer to the next node, and return the node's event type, the previous end node and the next start node will have a newline character, the line break is a text node,Type =Xp.next (); } for(city c:citylist) {System.out.println (c.tostring ()); } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}/*<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?><weather> <city> <NAME>QQ&L t;/name> <temp>5</temp> <pm>80</pm> </city> <city> <n ame>rr</name> <temp>-5</temp> <pm>800</pm> </city> <city> <name>???? </name> <temp>12?? </temp> <pm>60</pm> </city></weather>*/
Android 67 generate and parse XML