第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

Java系列:讀取XML文件

標(biāo)簽:
Java
  • 导入jar包
    下载jdom-2.0.6.zip并导入jdom-2.0.6.jar
  • xml源文件
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <AlarmList Type="1" Key="0101" Description="瓦斯超限,赶快撤离1"></AlarmList>
  <AlarmList Type="1" Key="0102" Description="瓦斯超限,赶快撤离2"></AlarmList>
  <AlarmList Type="1" Key="0103" Description="瓦斯超限,赶快撤离3"></AlarmList>
</Root>
  • 代码实例
package json;

import java.io.*;
import java.util.List;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.json.JSONObject;

public class ReadXML {
    public String[]  readXML() {
        String dataPath = "E:\\GDKJ\\word\\sensor\\xml\\ExecData20180327102411.xml";
        String[] str = new String[0];
        SAXBuilder builder = new SAXBuilder();
        try {
            JSONObject jsonObj = new JSONObject("{}");
            File file = new File(dataPath);
            if (file.isFile() && file.exists()) {
                InputStreamReader in = new InputStreamReader(new FileInputStream(file), "GBK");
                //通过saxBuilder的build方法,将输入流加载到saxBuilder中
                Document document = builder.build(in);
                //通过document对象获取xml文件的根节点
                Element rootElement = document.getRootElement();
                //获取根节点下的子节点的List集合
                List<Element> bookList = rootElement.getChildren();
                int i =0;
                str = new String[bookList.size()];
                for (Element book : bookList) {
                    List<Attribute> attrBook = book.getAttributes();
                    // 解析AlarmList的属性集合
                    for (Attribute attr : attrBook) {
                        String attrName = attr.getName();
                        String attrValue = attr.getValue();
                        jsonObj.put(attrName,attrValue);
                    }
                    str[i] = String.valueOf(jsonObj);
                    i++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
}
  • 输出结果
    图片描述
點(diǎn)擊查看更多內(nèi)容
4人點(diǎn)贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
JAVA開發(fā)工程師
手記
粉絲
171
獲贊與收藏
888

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消