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 to generate the xml encoding format serializer. startDocument ("UTF-8", false); // sets the xml namespace and root element, which is used by any element to create serializer. startTag (null, "classes"); // you can specify the group attribute and specify the element attribute serializer. attribute (null, "name", ""); // create the value of the xml element serializer. text ("80 80"); // Create an xml element to end 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
The above is the content of the xml file created by android pull. For more information, see PHP Chinese website (www.php1.cn )!