本文实例讲述了androID编程之xml文件读取和写入方法。分享给大家供大家参考。具体分析如下:
一、环境:
主机:WIN8
开发环境:Eclipse
二、说明:
1.打开sd卡中的xml文件,如果不存在,这新建一个,并写入默认配置
2.读取xml文件
三、xml文件格式:
<?xml version="1.0" enCoding="UTF-8" standalone="true"?> -<config> <Title>远程视频会见系统</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(); /** * 构造函数 */ public Config() { boolean ok; file sd_path; file file_cfg_dir; file file_cfg; fileOutputStream out; String str; fileinputStream in; //得到本机IP地址 config_info.local_ip = getLocalipAddress(); System.out.printf("本机IP:%s\n",config_info.local_ip); //获取SD卡目录 sd_path = Environment.getExternalStorageDirectory(); //判断文件夹是否存在 file_cfg_dir = new file(sd_path.getPath() + "//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("创建文件夹成功!"); } else { System.out.println("创建文件夹失败!"); } } //判断配置文件是否存在 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("创建文件cfg.xml成功!"); //生成初始化的配置数据 try { out = new fileOutputStream(file_cfg); //保存默认配置 config_info.Title = "远程视频会见系统"; 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 = "远程"; 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); // 获取根节点 Element root = document.getdocumentElement(); NodeList node = root.getChildNodes(); //获得第1子节点:标题 config_info.Title = node.item(0).getFirstChild().getNodeValue(); //获得第2子节点:本机端口 config_info.local_port = Integer.parseInt(node.item(1).getFirstChild().getNodeValue()); //获得第3子节点:调度服务器ip config_info.schedule_server_ip = node.item(2).getFirstChild().getNodeValue(); //获得第4子节点:调度服务器端口 config_info.schedule_server_port = Integer.parseInt(node.item(3).getFirstChild().getNodeValue()); System.out.printf("----222222222%s,config_info.schedule_server_port); } catch (Exception e) { e.printstacktrace(); } } @OverrIDe public Config_Info get_config_info() { return config_info; } /** * 得到本机IP地址 * @return 本机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数据流 * Config_Info的本机IP信息不会保存 * @param info:配置信息 * @return xml的String数据流 */ private String produce_xml_string(Config_Info info) { StringWriter stringWriter = new StringWriter(); try { // 获取XmlSerializer对象 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlSerializer xmlSerializer = factory.newSerializer(); // 设置输出流对象 xmlSerializer.setoutput(stringWriter); //开始标签 xmlSerializer.startdocument("utf-8",true); xmlSerializer.startTag(null,"config"); //标题 xmlSerializer.startTag(null,"Title"); xmlSerializer.text(info.Title); xmlSerializer.endTag(null,"Title"); //本机端口 xmlSerializer.startTag(null,"local_port"); xmlSerializer.text(Integer.toString(info.local_port)); xmlSerializer.endTag(null,"local_port"); //调度服务器ip xmlSerializer.startTag(null,"schedule_service_ip"); xmlSerializer.text(info.schedule_server_ip); xmlSerializer.endTag(null,"schedule_service_ip"); //调度服务器端口 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程序设计有所帮助。
总结以上是内存溢出为你收集整理的android编程之xml文件读取和写入方法全部内容,希望文章能够帮你解决android编程之xml文件读取和写入方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)