【Android】XML文件读取

需求

在系统应用中存在配置文件,包含了应用运行所需要的默认数据,此数据保存在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;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值