android编程之xml文件读取和写入方法
作者:jdh99 时间:2022-08-23 05:47:48
本文实例讲述了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,%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;
}
/**
* 得到本机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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android编程实现图片平铺的方法分析
2022-05-27 06:37:40
Android中一种巧妙的drawable.xml替代方案分享
2021-12-06 04:18:35
![](https://img.aspxhome.com/file/2023/9/138829_0s.png)
Android基于OpenCV实现图像修复
2022-04-28 00:51:39
![](https://img.aspxhome.com/file/2023/8/137868_0s.png)
WinForm实现的图片拖拽与缩放功能示例
2021-06-18 09:17:26
![](https://img.aspxhome.com/file/2023/9/78179_0s.png)
C++作用域与函数重载的实现
2022-04-30 06:28:30
![](https://img.aspxhome.com/file/2023/7/122587_0s.png)
解决spring boot启动扫描不到自定义注解的问题
2023-10-29 14:31:48
Android setTag方法的key问题解决办法
2021-09-12 14:29:37
Android开发实现的ViewPager引导页功能(动态加载指示器)详解
2021-10-16 17:40:35
![](https://img.aspxhome.com/file/2023/5/138205_0s.gif)
Java图形用户界面设计(Swing)的介绍
2022-08-23 03:29:37
![](https://img.aspxhome.com/file/2023/4/75654_0s.jpg)
Android Hilt依赖注入的使用讲解
2023-11-23 02:13:50
详解IntelliJ IDEA 自带的 HTTP Client 接口调用插件吊打 Postman
2023-08-06 07:56:08
![](https://img.aspxhome.com/file/2023/7/84327_0s.jpg)
Java实现在线聊天室(层层递进)
2022-06-13 11:03:47
![](https://img.aspxhome.com/file/2023/3/91093_0s.jpg)
详解Android中Handler的内部实现原理
2023-05-11 12:50:12
![](https://img.aspxhome.com/file/2023/0/101860_0s.png)
shiro之记住登录信息
2023-03-06 18:39:13
Mybatis的详细使用教程
2022-02-15 21:08:20
c#中分割字符串的几种方法
2023-04-11 16:04:30
Android四大组件之BroadcastReceiver详解
2023-03-17 02:55:08
JavaCV调用百度AI实现人脸检测方法详解
2023-01-09 01:16:09
![](https://img.aspxhome.com/file/2023/4/81234_0s.png)
C# 输出参数out问题
2023-02-27 00:19:32
java实战之飞机大战小游戏(源码加注释)
2021-08-01 17:46:08
![](https://img.aspxhome.com/file/2023/5/63485_0s.png)