'android xml 파일 만들기'에 해당되는 글 1건

  1. 2014.06.20 [Android] XML 파일 생성하기
2014.06.20 17:00


package com.XmlFileCreator;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;


import org.xmlpull.v1.XmlSerializer;


import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.util.Xml;

import android.widget.TextView;


public class XmlFileCreator extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //create a new file called "new.xml" in the SD card

        File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new.xml");

        try{

                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, "root");

                        //i indent code just to have a view similar to xml-tree

                                serializer.startTag(null, "child1");

                                serializer.endTag(null, "child1");

                               

                                serializer.startTag(null, "child2");

                                //set an attribute called "attribute" with a "value" for 

                                serializer.attribute(null, "attribute", "value");

                                serializer.endTag(null, "child2");

                       

                                serializer.startTag(null, "child3");

                                //write some text inside 

                                serializer.text("some text inside child3");

                                serializer.endTag(null, "child3");

                               

                        serializer.endTag(null, "root");

                        serializer.endDocument();

                        //write xml data into the FileOutputStream

                        serializer.flush();

                        //finally we close the file stream

                        fileos.close();

                       

                TextView tv = (TextView)this.findViewById(R.id.result);

                        tv.setText("file has been created on SD card");

                } catch (Exception e) {

                        Log.e("Exception","error occurred while creating xml file");

                }

    }

} 


결과는 다음과 같습니다.


<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>

<root>

  <child1 />

  <child2 attribute="value" />

  <child3>some text inside child3</child3>

</root>


출처 : http://www.anddev.org/write_a_simple_xml_file_in_the_sd_card_using_xmlserializer-t8350.html


신고
Posted by 그린블로그 DEV그린