一、环境:
主机:WIN8
开发环境:Eclipse
二、说明:
1打开sd卡中的xml文件,如果不存在,这新建一个,并写入默认配置
2读取xml文件
三、xml文件格式:
<xml version="10" encoding="UTF-8" standalone="true">
-<config>
<title>远程视频会见系统</title>
<local_port>12600</local_port>
<schedule_service_ip>1058159</schedule_service_ip>
<schedule_service_port>12601</schedule_service_port>
</config>
四、源代码:
package comexamplehelloanychat;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
import javaioStringWriter;
import javanetInet6Address;
import javanetInetAddress;
import javanetNetworkInterface;
import javanetSocketException;
import javautilEnumeration;
import javaxxmlparsersDocumentBuilder;
import javaxxmlparsersDocumentBuilderFactory;
import androidosEnvironment;
import androidutilLog;
import orgw3cdomDocument;
import orgw3cdomElement;
import orgw3cdomNodeList;
import orgxmlpullv1XmlPullParserFactory;
import orgxmlpullv1XmlSerializer;
/
配置信息类
新建日期: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_infolocal_ip = getLocalIpAddress();
Systemoutprintf("本机ip:%s\n", config_infolocal_ip);
//获取SD卡目录
sd_path = EnvironmentgetExternalStorageDirectory();
//判断文件夹是否存在
file_cfg_dir = new File(sd_pathgetPath() + "//Remote_Meeting");
if (!file_cfg_direxists() && !file_cfg_dirisDirectory()) {
Systemoutprintln("配置文件夹Remote_Meeting不存在!");
ok = file_cfg_dirmkdirs();
if (ok) {
Systemoutprintln("创建文件夹成功!");
} else {
Systemoutprintln("创建文件夹失败!");
}
}
//判断配置文件是否存在
file_cfg = new File(file_cfg_dirgetPath(),"cfgxml");
if (!file_cfgexists())
{
Systemoutprintln("配置文件cfgxml不存在!");
try {
file_cfgcreateNewFile();
Systemoutprintln("创建文件cfgxml成功!");
//生成初始化的配置数据
try {
out = new FileOutputStream(file_cfg);
//保存默认配置
config_infotitle = "远程视频会见系统";
config_infolocal_port = 12600;
config_infoschedule_server_ip = "1058159";
config_infoschedule_server_port = 12601;
str = produce_xml_string(config_info);
outwrite(strgetBytes());
outclose();
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
config_infotitle = "远程";
config_infolocal_port = 126;
config_infoschedule_server_ip = "105";
config_infoschedule_server_port = 12;
Systemoutprintf("----222222222%s,%d,%s,%d\n",config_infotitle,config_infolocal_port,
config_infoschedule_server_ip,config_infoschedule_server_port);
//解析xml文件
try {
in = new FileInputStream(file_cfg);
DocumentBuilderFactory factory = DocumentBuilderFactorynewInstance();
DocumentBuilder builder = factorynewDocumentBuilder();
Document document = builderparse(in);
// 获取根节点
Element root = documentgetDocumentElement();
NodeList node = rootgetChildNodes();
//获得第1子节点:标题
config_infotitle = nodeitem(0)getFirstChild()getNodeValue();
//获得第2子节点:本机端口
config_infolocal_port = IntegerparseInt(nodeitem(1)getFirstChild()getNodeValue());
//获得第3子节点:调度服务器ip
config_infoschedule_server_ip = nodeitem(2)getFirstChild()getNodeValue();
//获得第4子节点:调度服务器端口
config_infoschedule_server_port = IntegerparseInt(nodeitem(3)getFirstChild()getNodeValue());
Systemoutprintf("----222222222%s,%d,%s,%d\n",config_infotitle,config_infolocal_port,
config_infoschedule_server_ip,config_infoschedule_server_port);
} catch (Exception e) {
eprintStackTrace();
}
}
@Override
public Config_Info get_config_info() {
return config_info;
}
/
得到本机ip地址
@return 本机ip地址
/
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
getNetworkInterfaces(); enhasMoreElements();) {
NetworkInterface intf = ennextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
getInetAddresses(); enumIpAddrhasMoreElements();) {
InetAddress inetAddress = enumIpAddrnextElement();
//if (!inetAddressisLoopbackAddress()) {
if (!inetAddressisLoopbackAddress() && !(inetAddress instanceof Inet6Address)) {
return inetAddressgetHostAddress()toString();
}
}
}
} catch (SocketException ex) {
Loge("WifiPreference IpAddress", extoString());
}
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 = XmlPullParserFactorynewInstance();
XmlSerializer xmlSerializer = factorynewSerializer();
// 设置输出流对象
xmlSerializersetOutput(stringWriter);
//开始标签
xmlSerializerstartDocument("utf-8", true);
xmlSerializerstartTag(null, "config");
//标题
xmlSerializerstartTag(null, "title");
xmlSerializertext(infotitle);
xmlSerializerendTag(null, "title");
//本机端口
xmlSerializerstartTag(null, "local_port");
xmlSerializertext(IntegertoString(infolocal_port));
xmlSerializerendTag(null, "local_port");
//调度服务器ip
xmlSerializerstartTag(null, "schedule_service_ip");
xmlSerializertext(infoschedule_server_ip);
xmlSerializerendTag(null, "schedule_service_ip");
//调度服务器端口
xmlSerializerstartTag(null, "schedule_service_port");
xmlSerializertext(IntegertoString(infoschedule_server_port));
xmlSerializerendTag(null, "schedule_service_port");
xmlSerializerendTag(null, "config");
xmlSerializerendDocument();
} catch (Exception e) {
eprintStackTrace();
}
return stringWritertoString();
}
}
1、确保在清单文件中添加如下权限:<!-- 使用网络功能所需权限 --> <uses-permission android:name="androidpermissionINTERNET" > </uses-permission>
2、如果已经添加了该权限,并且编写测试类能够获取,但在Android项目中运行时却提示:
3、则可能是连接的模拟器或手机‘网络没有连接’……已经亲验。
按以下代码可获取到list数据:
package comexamplesdtgsdsw;
import javaioIOException;
import javautilArrayList;
import javautilHashMap;
import javautilList;
import javautilMap;
import orgksoap2SoapEnvelope;
import orgksoap2SoapFault;
import orgksoap2serializationSoapObject;
import orgksoap2serializationSoapSerializationEnvelope;
import orgksoap2transport>
以上就是关于android怎么从xml文件里面提取数据全部的内容,包括:android怎么从xml文件里面提取数据、android怎么使用api获取数据、android 怎么取list数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)