Android Dom Way to create XML

Source: Internet
Author: User

http://blog.csdn.net/nxh_love/article/details/7085174

In the Android DOM parsing xml article, the application of DOM parsing XML is briefly introduced. On the basis of the original article today, say something about the application of Dom in Android to create XML.

First: The created file will be placed under/DATA/DATA/CN.COM.XXX (current package name)/files.

Create the resulting XML file as follows:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <classes>
  3. <group name= "first grade" num= "ten">
  4. <person name= "Xiao Ming" age="7">
  5. <Chinese> Language</Chinese>
  6. <中文版> English</中文版>
  7. </person>
  8. </Group>
  9. </Classes>

Can directly use the Android DOM parsing xml way to parse the article, pay attention to modify one point:

[HTML]View Plaincopy
    1. //  get file from Assets folder   convert to input stream   
    2. //           instream = this.getresources (). Getassets (). open (fileName);   
    3. //          doc  = docbuilder.parse (instream);   
    4.             InputStream  fosstream = openfileinput (fileName);   
    5.              doc = docbuilder.parse (fosStream);   

At the same time, FileName gets the method:

[HTML]View Plaincopy
    1. string[] fileNames = getfilesdir (). List ();
[HTML]View Plaincopy
    1. String fileName = filenames[0];

The result of the analysis is


Here's the code to create the XML file:

[HTML]View Plaincopy
    1. private void Createxmlfile () {
    2. try {
    3. Documentbuilderfactory factory = documentbuilderfactory
    4. . newinstance ();
    5. Documentbuilder builder = Factory.newdocumentbuilder ();
    6. Document doc = builder.newdocument ();
    7. Creating an XML root element
    8. Element Rootele = doc.createelement ("classes");
    9. Doc.appendchild (Rootele);
    10. Creating an XML Level two element
    11. Element Groupele = doc.createelement ("group");
    12. Groupele.setattribute ("name", "first grade");
    13. Groupele.setattribute ("num", "10");
    14. Creating an XML person element
    15. Element Personele = doc.createelement ("person");
    16. Properties and property values for Personele
    17. Personele.setattribute ("name", "Xiaoming");
    18. Personele.setattribute ("Age", "7");
    19. To create a personele child element
    20. Element Chinese = doc.createelement ("Chinese");
    21. Create a value for a personele child element
    22. Chinese.appendchild (Doc.createtextnode ("Language 90"));
    23. Personele.appendchild (Chinese);
    24. Element中文版 = doc.createelement ("中文版");
    25. English.appendchild (Doc.createtextnode ("English 80"));
    26. Personele.appendchild (中文版);
    27. Groupele.appendchild (Personele);
    28. Rootele.appendchild (Groupele);
    29. Transformerfactory tf = transformerfactory.newinstance ();
    30. Transformer Transformer = Tf.newtransformer ();
    31. Domsource Source = new Domsource (DOC);
    32. Transformer.setoutputproperty (outputkeys.encoding, "utf-8");
    33. Transformer.setoutputproperty (Outputkeys.indent, "no");
    34. Create files stored in/DATA/DATA/CN.XXX.XXX (current package)/files
    35. FileOutputStream fos = openfileoutput ("Dom.xml", context.mode_private);
    36. Create files stored in/DATA/DATA/CN.XXX.XXX (current package)/cache
    37. FileOutputStream fos = Op
    38. PrintWriter pw = new PrintWriter (FOS);
    39. Streamresult result = new Streamresult (PW);
    40. Transformer.transform (source, result);
    41. SYSTEM.OUT.PRINTLN ("Generate XML file successfully!");
    42. } catch (Parserconfigurationexception e) {
    43. System.out.println (E.getmessage ());
    44. } catch (Transformerconfigurationexception e) {
    45. System.out.println (E.getmessage ());
    46. } catch (Transformerexception e) {
    47. System.out.println (E.getmessage ());
    48. } catch (FileNotFoundException e) {
    49. System.out.println (E.getmessage ());
    50. }
    51. }

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.