Write read XML in Android

Source: Internet
Author: User

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
  1. XML data Generation
  2. private String writetostring () {
  3. XmlSerializer serializer = Xml.newserializer ();
  4. StringWriter writer = new StringWriter ();
  5. try {
  6. Serializer.setoutput (writer);
  7. Serializer.startdocument ("Utf-8", true);
  8. Serializer.starttag ("", "users");
  9. Serializer.starttag ("", "UserName");
  10. Serializer.text (Txtuser.gettext (). toString ());
  11. Serializer.endtag ("", "UserName");
  12. Serializer.starttag ("", "UserEmail");
  13. Serializer.text (Txtemail.gettext (). toString ());
  14. Serializer.endtag ("", "UserEmail");
  15. Serializer.starttag ("", "PassWord");
  16. Serializer.text (Txtpass.gettext (). toString ());
  17. Serializer.endtag ("", "PassWord");
  18. Serializer.endtag ("", "users");
  19. Serializer.enddocument ();
  20. } catch (IllegalArgumentException e) {
  21. //Todo:handle exception
  22. E.printstacktrace ();
  23. } catch (IllegalStateException e) {
  24. //Todo:handle exception
  25. E.printstacktrace ();
  26. } catch (IOException e) {
  27. //Todo:handle exception
  28. E.printstacktrace ();
  29. }
  30. return writer.tostring ();
  31. }
  32. //Save the string as a private file of the APK
  33. Private Boolean writetoxml (String str) {
  34. try {
  35. OutputStream out = Openfileoutput ("Users.xml", mode_private);
  36. OutputStreamWriter outwriter = new OutputStreamWriter (out);
  37. try {
  38. Outwriter.write (str);
  39. Outwriter.close ();
  40. Out.close ();
  41. return true;
  42. } catch (IOException e) {
  43. //Todo:handle exception
  44. return false;
  45. }
  46. } catch (Exception e) {
  47. //Todo:handle exception
  48. return false;
  49. }
  50. }

When called:

if (Writetoxml (writetostring ()))

{

Success

}


Data Read xml:

[Java]View Plaincopy
    1. tag, the node name deposited
[Java]View Plaincopy
    1. private string Readxmluser (String tag)
    2. {
    3. String re="";
    4. Documentbuilderfactory documentbuilderfactory;
    5. Documentbuilder Documentbuilder;
    6. Document document;
    7. try {
    8. Documentbuilderfactory=documentbuilderfactory.newinstance ();
    9. Documentbuilder=documentbuilderfactory.newdocumentbuilder ();
    10. //xml files into the assets directory
    11. Document=documentbuilder.parse (this.openfileinput ("Users.xml"));
    12. Org.w3c.dom.Element root= document.getdocumentelement ();
    13. NodeList nodelist=root.getelementsbytagname (tag);
    14. Node Nd=nodelist.item (0);
    15. Re= nd.getfirstchild (). Getnodevalue ();
    16. } catch (Exception e) {
    17. //Todo:handle exception
    18. E.printstacktrace ();
    19. }finally{
    20. document=null;
    21. documentbuilder=null;
    22. documentbuilderfactory=null;
    23. }
    24. return re;
    25. }

http://blog.csdn.net/hanjingjingpan/article/details/8812770

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.