Android创建XMl文件 .

  1. *生成xml格式字符串*/  
  2. public String WriteXmlStr()  
  3. {  
  4.     XmlSerializer serializer = Xml.newSerializer();  
  5.     StringWriter writer = new StringWriter();  
  6.     try  
  7.     {  
  8.         serializer.setOutput(writer);  
  9.         serializer.startDocument("UTF-8",true);  
  10.         serializer.startTag("","project");  
  11.         serializer.attribute("","date","2011-01-15");  
  12.           
  13.           serializer.startTag("","graphics");  
  14.             serializer.comment("**********注释***********");  
  15.               
  16.             serializer.startTag("","graphics1");  
  17.                serializer.startTag("","name");  
  18.                serializer.text("logo name");  
  19.                serializer.endTag("","name");  
  20.                serializer.cdsect("cdata数据");  
  21.             serializer.endTag("","graphics1");  
  22.               
  23.             serializer.startTag("","graphics2");  
  24.                serializer.startTag("","name");  
  25.                serializer.text("logo name");  
  26.                serializer.endTag("","name");  
  27.                serializer.cdsect("cdata数据");  
  28.             serializer.endTag("","graphics2");  
  29.   
  30.           serializer.endTag("","graphics");  
  31.         serializer.endTag("","project");  
  32.           
  33.         serializer.endDocument();  
  34.         return writer.toString();  
  35.     }  
  36.     catch(Exception e)  
  37.     {  
  38.       throw new RuntimeException(e);  
  39.     }  
  40. }  
  41.   
  42. /*将字符串写入私有文件夹下 文件存放在data/data/package/files*/  
  43. public void WriteFileData(String fileName,String message)  
  44. {  
  45.     try  
  46.     {  
  47.        FileOutputStream fout =openFileOutput(fileName, android.content.Context.MODE_PRIVATE);  
  48.        byte [] bytes = message.getBytes();  
  49.        fout.write(bytes);  
  50.        fout.close();  
  51.     }  
  52.     catch(Exception e){  
  53.      e.printStackTrace();  
  54.     }  
  55. }    
  56.   
  57. /*调用格式WriteFileData("xmlTest.xml",WriteXmlStr())*/  
/*生成xml格式字符串*/ public String WriteXmlStr() { XmlSerializer serializer = Xml.newSerializer(); StringWriter writer = new StringWriter(); try { serializer.setOutput(writer); serializer.startDocument("UTF-8",true); serializer.startTag("","project"); serializer.attribute("","date","2011-01-15"); serializer.startTag("","graphics"); serializer.comment("**********注释***********"); serializer.startTag("","graphics1"); serializer.startTag("","name"); serializer.text("logo name"); serializer.endTag("","name"); serializer.cdsect("cdata数据"); serializer.endTag("","graphics1"); serializer.startTag("","graphics2"); serializer.startTag("","name"); serializer.text("logo name"); serializer.endTag("","name"); serializer.cdsect("cdata数据"); serializer.endTag("","graphics2"); serializer.endTag("","graphics"); serializer.endTag("","project"); serializer.endDocument(); return writer.toString(); } catch(Exception e) { throw new RuntimeException(e); } } /*将字符串写入私有文件夹下 文件存放在data/data/package/files*/ public void WriteFileData(String fileName,String message) { try { FileOutputStream fout =openFileOutput(fileName, android.content.Context.MODE_PRIVATE); byte [] bytes = message.getBytes(); fout.write(bytes); fout.close(); } catch(Exception e){ e.printStackTrace(); } } /*调用格式WriteFileData("xmlTest.xml",WriteXmlStr())*/

 

输出文件

 

[xhtml] view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>   
  2. <project date="2011-01-15">  
  3.   - <graphics>  
  4.   - <!-- **********注释***********-->   
  5.     - <graphics1>  
  6.         <name>logo name</name>   
  7.       - <![CDATA[ cdata数据]]>   
  8.       </graphics1>  
  9.     - <graphics2>  
  10.         <name>logo name</name>   
  11.       - <![CDATA[ cdata数据]]>   
  12.       </graphics2>  
  13.     </graphics>  
  14.   </project> 

你可能感兴趣的:(xml,exception,android,String,XHTML,encoding)