【Android】创建、读取XML文件
16lz
2021-01-23
创建:
package webdomain;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.StringWriter;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xmlpull.v1.XmlPullParserFactory;import org.xmlpull.v1.XmlSerializer;import form.FrmLogin;import form.FrmShintai;import android.os.Environment;import android.util.Log;import android.util.Xml;import android.widget.ArrayAdapter; public class Java2XML { public static void XmlFileCreator(String abc){ File newxmlfile = new File(FrmLogin.SDCARD_PATH+"***"); try{ if(!newxmlfile.exists()) newxmlfile.createNewFile(); }catch(IOException e){ Log.e("IOException", "exception in createNewFile() method"); } //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"); } // 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, null); //set indentation option serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer.startTag(null, "jokes"); serializer.startTag(null, "tag"); serializer.startTag(null, "123"); serializer.text(FrmS.G_Itmho.getText().toString()); serializer.endTag(null, "123"); serializer.startTag(null, "456"); serializer.text(FrmS.G_Itmu.getText().toString()); serializer.endTag(null, "456"); serializer.startTag(null, "789"); serializer.text(FrmSai.G_Itm.getText().toString()); serializer.endTag(null, "789"); serializer.endTag(null, "tag"); serializer.endTag(null, "jokes"); serializer.endDocument(); serializer.flush(); fileos.close(); } catch (Exception e) { Log.e("Exception","error occurred while creating xml file"); } }}
读取:
package xmldomain;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.kobjects.util.Strings;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import android.R.string;import form.FrmLogin;import form.FrmShintai;public class ReadXML {public static void ReadXMLs(){File newxmlfile = new File(FrmLogin.SDCARD_PATH+"filepath");FileInputStream fileos = null;try {fileos = new FileInputStream(newxmlfile);} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}DocumentBuilderFactory docBuilderFactory = null;DocumentBuilder docBuilder = null;Document doc = null;try{docBuilderFactory =DocumentBuilderFactory.newInstance();docBuilder =docBuilderFactory.newDocumentBuilder();//xml file doc = docBuilder.parse(fileos);//root elementElement root = doc.getDocumentElement();//Do something here//Get a NodeList by tagnameNodeList shenchang = root.getElementsByTagName_r("123");Node nd = shenchang.item(0);FrmShintai.G_ItmZenkaiShincho.setText(nd.getTextContent());//Read NodeNodeList tizhong = root.getElementsByTagName_r("456");Node nd1 = tizhong.item(0);FrmShintai.G_ItmZenkaiTaiju.setText(nd1.getTextContent());//Read NodeNodeList bmi = root.getElementsByTagName_r("789");Node nd2 = bmi.item(0);FrmShintai.G_ItemZenkaiBMI.setText(nd2.getTextContent());//Read Node2}catch(IOException e){}catch(SAXException e){}catch(ParserConfigurationException e){}finally{doc =null;docBuilder=null;docBuilderFactory=null;}}}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer