需求
在系统应用中存在配置文件,包含了应用运行所需要的默认数据,此数据保存在xml文件中,需要应用读取此xml文件获取到数据。
XML文件样式
设置xml文件名为【set.xml】
<?xml version="1.0" encoding="utf-8"?>
<accent>
<!-- 配置数据 -->
<ip>192.168.1.104</ip>
<port>0001</port>
<user>2096</user>
<password>2096</password>
</accent>
读取配置信息
设置一个Bean类保存信息
public class MySetBean {
private String ip;
private String port;
private String user;
private String password;
public MySetBean() {
}
public MySetBean(String ip, String port, String user, String password) {
this.ip = ip;
this.port = port;
this.user = user;
this.password = password;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "MySetBean{" +
"ip='" + ip + '\'' +
", port='" + port + '\'' +
", user='" + user + '\'' +
", password='" + password + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MySetBean mySetBean = (MySetBean) o;
return Objects.equals(ip, mySetBean.ip) && Objects.equals(port, mySetBean.port) && Objects.equals(user, mySetBean.user) && Objects.equals(password, mySetBean.password);
}
@Override
public int hashCode() {
return Objects.hash(ip, port, user, password);
}
}
读取xml文件数据
public class XMLUtil {
public MySetBean ReadXML(Context context, String sXMLFile) {
MySetBean setBean = new MySetBean();
try {
//1.获得DOM解析器的工厂示例:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//2.从Dom工厂中获得dom解析器
DocumentBuilder builder = dbFactory.newDocumentBuilder();
//3.使用文件的路径+文件名生成File对象
File tempFile = new File(sXMLFile);
//如果存在
if (tempFile.exists()) {
// Toast.makeText(context, "正在读取系统配置文件", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "系统配置文件不存在", Toast.LENGTH_SHORT).show();
return null;
}
FileInputStream inputStream = new FileInputStream(tempFile);
Document document = null;
if (inputStream != null) {
document = builder.parse(inputStream);
}
//4.得到文档中名称为accent的元素的结点,文件中只有一个所以不需要遍历
NodeList nodeList = document.getElementsByTagName("accent").item(0).getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
//读取配置信息
if (nodeList.item(i).getNodeName().equals("ip")) {
setBean.setIp(nodeList.item(i).getFirstChild().getNodeValue().trim());
}
if (nodeList.item(i).getNodeName().equals("port")) {
setBean.setPort(nodeList.item(i).getFirstChild().getNodeValue().trim());
}
if (nodeList.item(i).getNodeName().equals("user")) {
setBean.setUser(nodeList.item(i).getFirstChild().getNodeValue().trim());
}
if (nodeList.item(i).getNodeName().equals("password")) {
setBean.setPwd(nodeList.item(i).getFirstChild().getNodeValue().trim());
}
}
}
//关闭文件
inputStream.close();
tempFile = null;
} catch (Exception e) {
e.printStackTrace();
}
return setBean;
}
}