Android中XmlSerializer產生xml檔案

來源:互聯網
上載者:User

例1

 代碼如下 複製代碼

try {

  File f = new File(getExternalCacheDir().getAbsolutePath()+"my.xml");
OutputStream outPut = new FileOutputStream(f);

   XmlSerializer serializer=Xml.newSerializer(); 

   serializer.setOutput(outPut, "utf-8"); 

   

   serializer.startDocument("utf-8", true); 

   serializer.startTag(null, "companys"); 

   

   for(String[] s:taxiCompany) 

   { 

      serializer.startTag(null, DBUtil.TAXI_TABLE); 

     

      serializer.attribute(null, DBUtil.KEY_PROVINCE, s[0]); 

      serializer.attribute(null, DBUtil.KEY_CITYNAME, s[1]);

      serializer.attribute(null, DBUtil.KEY_NAME, s[2]); 

      serializer.attribute(null, DBUtil.KEY_TELE, s[3]); 

       

      serializer.endTag(null, DBUtil.TAXI_TABLE); 

   } 

   

   serializer.endTag(null, "companys"); 

   serializer.endDocument(); 

   outPut.close();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

執行個體2

 代碼如下 複製代碼

private static void XmlFileCreator(List<JokeBean> data){
        File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new.xml");
        try{
            if(!newxmlfile.exists())
                newxmlfile.createNewFile();
        }catch(IOException e){
            Log.e("IOException", "exception in createNewFile() method");
        }
        //we have to bind the new file with a FileOutputStream
        FileOutputStream fileos = null;       
        try{
            fileos = new FileOutputStream(newxmlfile);
        }catch(FileNotFoundException e){
            Log.e("FileNotFoundException", "can't create FileOutputStream");
        }
        //we create a XmlSerializer in order to write xml data
        XmlSerializer serializer = Xml.newSerializer();
        try {
            //we set the FileOutputStream as output for the serializer, using UTF-8 encoding
            serializer.setOutput(fileos, "UTF-8");
            //Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
            serializer.startDocument(null, Boolean.valueOf(true));
            //set indentation option
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
          //start a tag called "root"
            serializer.startTag(null, "jokes");
            for(JokeBean joke:data){
                serializer.startTag(null, "joke");
                //i indent code just to have a view similar to xml-tree
                serializer.startTag(null, "id");
                serializer.text(joke.getId());
                serializer.endTag(null, "id");
                                  
                serializer.startTag(null, "title");
                serializer.text(joke.getTitle());
                //set an attribute called "attribute" with a "value" for <child2>
                //serializer.attribute(null, "attribute", "value");
                serializer.endTag(null, "title");
                serializer.startTag(null, "text");
                //write some text inside <text>
                serializer.text(joke.getText());
                serializer.endTag(null, "text");
                                  
                serializer.endTag(null, "joke");
            }
            serializer.endTag(null, "jokes");
            serializer.endDocument();
            //write xml data into the FileOutputStream
            serializer.flush();
            //finally we close the file stream
            fileos.close();
        } catch (Exception e) {
            Log.e("Exception","error occurred while creating xml file");
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.