Androidpull: Create an xml file

Source: Internet
Author: User
The following auxiliary symbol pull for generating xml is not formatted, that is, no line breaks, tabs, and other formatting is easy to create an xml file,

// Set the generated xml encoding format

Serializer. startDocument ("UTF-8", false );
// Set the xml namespace and root element. any element is created in this way.
Serializer. startTag (null, "classes ");

// Set group attributes and Element attributes
Serializer. attribute (null, "name", "first grade ");

// Create the xml element value

Serializer. text ("language 80 ");

// The creation of the xml element ends.

Serializer. endTag (null, "person ");

The first parameter of the preceding method is the namespace of xml.

The following code generates simple xml:


Private void pullCreateXml () {try {OutputStream outStream = openFileOutput ("sax. xml ", Context. MODE_PRIVATE); XmlSerializer serializer = Xml. newSerializer (); // encoding format of the output stream serializer. setOutput (outStream, "UTF-8"); // sets the encoding format of the xml file, /* if the second parameter trues is used to generate tandalone = 'yes' * false, tandalone = 'no' * is generated. The value no indicates that the XML document is not independent, but depends on a DTD defined by the outside.. * the value "yes" indicates that the XML document is self-contained ). */serializer. startDocument ("UTF-8", false); // sets the xml namespace and the root element serializer. startTag (null, "classes"); serializer. startTag (null, "group"); // sets the serializer attribute of the group. attribute (null, "name", "first grade"); serializer. attribute (null, "num", "10"); serializer. startTag (null, "person"); serializer. attribute (null, "name", "James"); serializer. attribute (null, "age", "7"); serializer. startTag (null, "chinese"); serializer. text ("language 80"); serializer. endTag (null, "chinese"); serializer. startTag (null, "english"); serializer. text ("English 80"); serializer. endTag (null, "english"); serializer. endTag (null, "person"); serializer. endTag (null, "group"); serializer. endTag (null, "classes"); // write data to the xml file serializer. flush (); // Close the stream outStream. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}

The xml is not formatted, that is, the following auxiliary symbols are formatted, such as line breaks and tabs.



 
 
  
   
    
     
Language 80
    
    
     
English 80
    
   
  
 


Location of the generated xml file:


The above is the content of the xml file created by android pull. For more information, see PHP Chinese website (www.php1.cn )!

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.