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 )!