如何利用Android XmlSerializer生成XML文件
2015-11-25 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
用到的主要是XmlSerializer,利用它来写xml文件。
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");
}
}
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");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询