The basic idea of getting an XML file is to get the Xmlresourceparser object through the XML raw file obtained by Getresources (). GETXML (), which determines whether the beginning or end of a document is the beginning or the end of a tag, And through some methods of obtaining properties to traverse the XML file, so as to access the contents of the XML file, the following is an example of accessing the contents of the XML file, and display the content on a TextView
Data is written to XML:
Readxmltest.java
[Java]View Plaincopy
- XML data Generation
- private String writetostring () {
- XmlSerializer serializer = Xml.newserializer ();
- StringWriter writer = new StringWriter ();
- try {
- Serializer.setoutput (writer);
- Serializer.startdocument ("Utf-8", true);
- Serializer.starttag ("", "users");
- Serializer.starttag ("", "UserName");
- Serializer.text (Txtuser.gettext (). toString ());
- Serializer.endtag ("", "UserName");
- Serializer.starttag ("", "UserEmail");
- Serializer.text (Txtemail.gettext (). toString ());
- Serializer.endtag ("", "UserEmail");
- Serializer.starttag ("", "PassWord");
- Serializer.text (Txtpass.gettext (). toString ());
- Serializer.endtag ("", "PassWord");
- Serializer.endtag ("", "users");
- Serializer.enddocument ();
- } catch (IllegalArgumentException e) {
- //Todo:handle exception
- E.printstacktrace ();
- } catch (IllegalStateException e) {
- //Todo:handle exception
- E.printstacktrace ();
- } catch (IOException e) {
- //Todo:handle exception
- E.printstacktrace ();
- }
- return writer.tostring ();
- }
- //Save the string as a private file of the APK
- Private Boolean writetoxml (String str) {
- try {
- OutputStream out = Openfileoutput ("Users.xml", mode_private);
- OutputStreamWriter outwriter = new OutputStreamWriter (out);
- try {
- Outwriter.write (str);
- Outwriter.close ();
- Out.close ();
- return true;
- } catch (IOException e) {
- //Todo:handle exception
- return false;
- }
- } catch (Exception e) {
- //Todo:handle exception
- return false;
- }
- }
When called:
if (Writetoxml (writetostring ()))
{
Success
}
Data Read xml:
[Java]View Plaincopy
- tag, the node name deposited
[Java]View Plaincopy
- private string Readxmluser (String tag)
- {
- String re="";
- Documentbuilderfactory documentbuilderfactory;
- Documentbuilder Documentbuilder;
- Document document;
- try {
- Documentbuilderfactory=documentbuilderfactory.newinstance ();
- Documentbuilder=documentbuilderfactory.newdocumentbuilder ();
- //xml files into the assets directory
- Document=documentbuilder.parse (this.openfileinput ("Users.xml"));
- Org.w3c.dom.Element root= document.getdocumentelement ();
- NodeList nodelist=root.getelementsbytagname (tag);
- Node Nd=nodelist.item (0);
- Re= nd.getfirstchild (). Getnodevalue ();
- } catch (Exception e) {
- //Todo:handle exception
- E.printstacktrace ();
- }finally{
- document=null;
- documentbuilder=null;
- documentbuilderfactory=null;
- }
- return re;
- }
http://blog.csdn.net/hanjingjingpan/article/details/8812770