[Android] XML 파일 생성
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();
} catch (Exception e) {
Log.e("Exception","error occurred while creating xml file");
}
출처 : http://www.shop-wiz.com/document/android/execise_linkage_web_xml_create
'프로그래밍 > Android' 카테고리의 다른 글
[Android] WebView(웹뷰) 속성 (0) | 2015.03.26 |
---|---|
[Android] android 파일 다운로드 후 hwp파일 열기! (0) | 2015.03.26 |
[Android] if 값 비교가 제대로 안되는 경우 ex: if(a == b) (0) | 2015.03.16 |
[Android] Android 기본탐색기가 없다!! (0) | 2015.01.23 |
I/Choreographer(5855): Skipped 99 frames! The application may be doing too much work on its main thread. (0) | 2015.01.22 |