android編程之xml文件讀取和寫入方法
本文實例講述了android編程之xml文件讀取和寫入方法。分享給大家供大家參考。具體分析如下:
一、環(huán)境:
主機(jī):WIN8
開發(fā)環(huán)境:Eclipse
二、說明:
1.打開sd卡中的xml文件,如果不存在,這新建一個,并寫入默認(rèn)配置
2.讀取xml文件
三、xml文件格式:
<?xml version="1.0" encoding="UTF-8" standalone="true"?> -<config> <title>遠(yuǎn)程視頻會見系統(tǒng)</title> <local_port>12600</local_port> <schedule_service_ip>10.58.1.59</schedule_service_ip> <schedule_service_port>12601</schedule_service_port> </config>
四、源代碼:
package com.example.helloanychat; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.StringWriter; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import android.os.Environment; import android.util.Log; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlSerializer; /** * 配置信息類 * 新建日期:2014/12/8 by jdh */ public class Config implements IF_Config { //配置信息 private Config_Info config_info = new Config_Info(); /** * 構(gòu)造函數(shù) */ public Config() { boolean ok; File sd_path; File file_cfg_dir; File file_cfg; FileOutputStream out; String str; FileInputStream in; //得到本機(jī)ip地址 config_info.local_ip = getLocalIpAddress(); System.out.printf("本機(jī)ip:%s\n", config_info.local_ip); //獲取SD卡目錄 sd_path = Environment.getExternalStorageDirectory(); //判斷文件夾是否存在 file_cfg_dir = new File(sd_path.getPath() + "http://Remote_Meeting"); if (!file_cfg_dir.exists() && !file_cfg_dir.isDirectory()) { System.out.println("配置文件夾Remote_Meeting不存在!"); ok = file_cfg_dir.mkdirs(); if (ok) { System.out.println("創(chuàng)建文件夾成功!"); } else { System.out.println("創(chuàng)建文件夾失敗!"); } } //判斷配置文件是否存在 file_cfg = new File(file_cfg_dir.getPath(),"cfg.xml"); if (!file_cfg.exists()) { System.out.println("配置文件cfg.xml不存在!"); try { file_cfg.createNewFile(); System.out.println("創(chuàng)建文件cfg.xml成功!"); //生成初始化的配置數(shù)據(jù) try { out = new FileOutputStream(file_cfg); //保存默認(rèn)配置 config_info.title = "遠(yuǎn)程視頻會見系統(tǒng)"; config_info.local_port = 12600; config_info.schedule_server_ip = "10.58.1.59"; config_info.schedule_server_port = 12601; str = produce_xml_string(config_info); out.write(str.getBytes()); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } config_info.title = "遠(yuǎn)程"; config_info.local_port = 126; config_info.schedule_server_ip = "10.5"; config_info.schedule_server_port = 12; System.out.printf("----222222222%s,%d,%s,%d\n",config_info.title,config_info.local_port, config_info.schedule_server_ip,config_info.schedule_server_port); //解析xml文件 try { in = new FileInputStream(file_cfg); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(in); // 獲取根節(jié)點 Element root = document.getDocumentElement(); NodeList node = root.getChildNodes(); //獲得第1子節(jié)點:標(biāo)題 config_info.title = node.item(0).getFirstChild().getNodeValue(); //獲得第2子節(jié)點:本機(jī)端口 config_info.local_port = Integer.parseInt(node.item(1).getFirstChild().getNodeValue()); //獲得第3子節(jié)點:調(diào)度服務(wù)器ip config_info.schedule_server_ip = node.item(2).getFirstChild().getNodeValue(); //獲得第4子節(jié)點:調(diào)度服務(wù)器端口 config_info.schedule_server_port = Integer.parseInt(node.item(3).getFirstChild().getNodeValue()); System.out.printf("----222222222%s,%d,%s,%d\n",config_info.title,config_info.local_port, config_info.schedule_server_ip,config_info.schedule_server_port); } catch (Exception e) { e.printStackTrace(); } } @Override public Config_Info get_config_info() { return config_info; } /** * 得到本機(jī)ip地址 * @return 本機(jī)ip地址 */ private String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); //if (!inetAddress.isLoopbackAddress()) { if (!inetAddress.isLoopbackAddress() && !(inetAddress instanceof Inet6Address)) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("WifiPreference IpAddress", ex.toString()); } return null; } /** * 生成xml配置文件的String數(shù)據(jù)流 * Config_Info的本機(jī)ip信息不會保存 * @param info:配置信息 * @return xml的String數(shù)據(jù)流 */ private String produce_xml_string(Config_Info info) { StringWriter stringWriter = new StringWriter(); try { // 獲取XmlSerializer對象 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlSerializer xmlSerializer = factory.newSerializer(); // 設(shè)置輸出流對象 xmlSerializer.setOutput(stringWriter); //開始標(biāo)簽 xmlSerializer.startDocument("utf-8", true); xmlSerializer.startTag(null, "config"); //標(biāo)題 xmlSerializer.startTag(null, "title"); xmlSerializer.text(info.title); xmlSerializer.endTag(null, "title"); //本機(jī)端口 xmlSerializer.startTag(null, "local_port"); xmlSerializer.text(Integer.toString(info.local_port)); xmlSerializer.endTag(null, "local_port"); //調(diào)度服務(wù)器ip xmlSerializer.startTag(null, "schedule_service_ip"); xmlSerializer.text(info.schedule_server_ip); xmlSerializer.endTag(null, "schedule_service_ip"); //調(diào)度服務(wù)器端口 xmlSerializer.startTag(null, "schedule_service_port"); xmlSerializer.text(Integer.toString(info.schedule_server_port)); xmlSerializer.endTag(null, "schedule_service_port"); xmlSerializer.endTag(null, "config"); xmlSerializer.endDocument(); } catch (Exception e) { e.printStackTrace(); } return stringWriter.toString(); } }
希望本文所述對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
Android中利用C++處理Bitmap對象的實現(xiàn)方法
下面小編就為大家?guī)硪黄狝ndroid中利用C++處理Bitmap對象的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03Android開發(fā)控制ScrollView滑動速度的方法
這篇文章主要介紹了Android開發(fā)控制ScrollView滑動速度的方法,結(jié)合實例形式分析了Android編程中ScrollView滑動事件相關(guān)操作技巧,需要的朋友可以參考下2017-02-02Android編程實現(xiàn)全局獲取Context及使用Intent傳遞對象的方法詳解
這篇文章主要介紹了Android編程實現(xiàn)全局獲取Context及使用Intent傳遞對象的方法,結(jié)合實例形式分析了Android全局Context的獲取及Intent傳遞對象的具體操作方法,需要的朋友可以參考下2017-08-08Android Glide圖片加載(加載監(jiān)聽、加載動畫)
這篇文章主要為大家詳細(xì)介紹了Android Glide圖片加載的具體實現(xiàn)方法,包括加載監(jiān)聽、加載動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Android 通過代碼設(shè)置、打開wifi熱點及熱點連接的實現(xiàn)代碼
這篇文章主要介紹了Android 通過代碼設(shè)置、打開wifi熱點及熱點連接的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05kotlin object關(guān)鍵字單例模式實現(xiàn)示例詳解
這篇文章主要為大家介紹了kotlin object關(guān)鍵字單例模式實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01