Xml
If you have such an XML-formatted data:
<?xml version= "1.0" encoding= "Utf-8"?> <resources> <customer name= "Luopeng" age= "gender="
"1" emial= "dylankeepmoving@163.com"/>
<customer name= "Dylan" Age= "gender=" 2 "emial=" 710097663@ Qq.com "/>
<customer name=" Android "age=" 6 "gender=" 2 "emial=" android@gmail.com "/>
</resources >
Here we write a class to read, which is to display the contents of the resource file (an XML) on the EditView after clicking the button, which is used in the Xmlresourceparser
public class Testxmlresourceparseractivity extends activity {private edittext et;
Private Button MyButton;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
A variable is used in an inner class if the variable must be the final modified MyButton = (Button) This.findviewbyid (R.ID.BTN01);
ET = (edittext) This.findviewbyid (r.id.edittext01);
Mybutton.setonclicklistener (New Onclicklistener () {StringBuilder sb = new StringBuilder ("");
Resources res = getresources ();
Xmlresourceparser XRP = Res.getxml (r.xml.test);
@Override public void OnClick (View v) {int counter = 0; try {//To determine whether the end of the file while (Xrp.geteventtype ()!= xmlresourceparser.end_document) {//File contents Start tag, note that the starting tag here is the first label underneath the <resources> tag in the Test.xml file if (xrp.geteventtype () = = Xmlresourceparser.start_tag)
{String tagname = Xrp.getname (); if (Tagname.endswith ("Customer") {counter++;
Sb.append ("This is the first" + Counter + "customer" + "\ n");
Sb.append ("Name:" +xrp.getattributevalue (0) + "\ n");
Sb.append ("Age:" +xrp.getattributevalue (1) + "\ n");
Sb.append ("Sex:" +xrp.getattributevalue (2) + "\ n");
Sb.append ("Mailbox:" +xrp.getattributevalue (3) + "\ n");
} xrp.next ();
} et.settext (Sb.tostring ());
catch (Xmlpullparserexception e) {} catch (IOException e) {e.printstacktrace ();
}
}
});
}
Json
Creating JSON data
try {
//first creates a single key value in the object to
jsonobject root = new Jsonobject ();
Root.put ("Cat", "it");
The value of another key-value pair is an array with 3 JSON objects in it, so we'll get another 3 Jsonobject objects to hold the key value to
jsonobject lan1 = new Jsonobject ();
Lan1.put ("id", "1");
Lan1.put ("IDE", "Eclipse");
Lan1.put ("name", "Java");
Jsonobject lan2 = new Jsonobject ();
Lan2.put ("id", "2");
Lan2.put ("IDE", "XCode");
Lan2.put ("name", "Swift");
Jsonobject lan3 = new Jsonobject ();
Lan3.put ("id", "2");
Lan3.put ("IDE", "Visual Studio");
Lan3.put ("name", "C #");
The 3 objects are deposited into an array
jsonarray array = new Jsonarray ();
Array.put (lan1);
Array.put (lan2);
Array.put (LAN3);
The array is then used as the value of the key "languages" to form an object
root.put ("languages", array);
In this case, the content is only exported
System.out.println (root.tostring ());
} catch (Jsonexception e) {
e.printstacktrace ();
}
Reading JSON data
Assets/test.json
{"Languages": [{' id ': 1, IDE: Eclipse, ' name ': ' Java '}, {' id ': 2, ' ide ': ' XCode ', ' name ': ' Swift '}, {' id ': 3, ' IDE ": Visual Studio, Name:" C #}, "," cat "," it "} try {InputStream is = Getresources (). Getassets (). Open (" Test.json "
);
InputStreamReader ISR = new InputStreamReader (IS, "UTF-8");
BufferedReader br = new BufferedReader (ISR);
Reads the data in the text into a StringBuilder String line;
StringBuilder builder = new StringBuilder ();
while ((Line=br.readline ())!= null) {builder.append (line);
} br.close ();
Isr.close ();
Jsonobject root = new Jsonobject (builder.tostring ());
According to the key, get the value of the key, because the value is string type, so use getString System.out.println ("cat=" +root.getstring ("cat"));
According to the key, get the corresponding array of keys, because the value is an array, so use Getjsonarray jsonarray array = Root.getjsonarray ("languages"); Because you want to enumerate the groups, create a For loop for (int i=0 i < array. length (); i++) {//Because of the elements in each array, it is also a new JSON object Jsonobject LA
n = array.getjsonobject (i); System.out.priNtln ("-------------------");
System.out.println ("id=" +lan.getint ("id"));
System.out.println ("ide=" +lan.getstring ("name"));
System.out.println ("Name=" +lan.getstring ("name"));
LOG.I ("tag", "-------");
} catch (IOException e) {e.printstacktrace ();
catch (Jsonexception e) {//TODO auto-generated catch block E.printstacktrace ();
}