- private String writeXml(){
- XmlSerializer serializer = Xml.newSerializer();
- StringWriter writer = new StringWriter();
- try{
- serializer.setOutput(writer);
- // <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
- serializer.startDocument(”UTF-8″,true);
- // <blog number=”1″>
- serializer.startTag(”",”blog”);
- serializer.attribute(”",”number”,String.valueOf(1));
- //<message data=”2009-09-23″>
- serializer.startTag(”",”message”);
- serializer.attribute(”",”date”,”2009-09-23″);
- // <title>Android XML</title>
- serializer.startTag(”",”title”);
- serializer.text(”Android XML”);
- serializer.endTag(”",”title”);
- // <url>http://www.moandroid.com/?p=508 </url>
- serializer.startTag(”",”url”);
- serializer.text(”http://www.moandroid.com/?p=508″);
- serializer.endTag(”",”url”);
- //</message>
- serializer.endTag(”",”message”);
- // </blog>
- serializer.endTag(”",”blog”);
- serializer.endDocument();
- return writer.toString();
- }
- catch(Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
- <blog number=”1″>
- <message data=”2009-09-23″>
- <title>Android XML</title>
- <url>http://www.moandroid.com/?p=508</url>
- </message>
- </blog>
- public boolean Write(String path,String txt)
- {
- try
- {
- OutputStream os = openFileOutput(path,MODE_PRIVATE);
- OutputStreamWriter osw=new OutputStreamWriter(os);
- osw.write(txt);
- osw.close();
- os.close();
- }
- catch(FileNotFoundException e)
- {
- return false;
- }
- catch(IOException e)
- {
- return false;
- }
- return true;
- }
- }
联系客服