zoukankan      html  css  js  c++  java
  • 01_1_jdom调用xml文件

    01_1_jdom调用xml文件

    1. 导入jdom.jar

    2. xml文件内容

    test.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <HD>
    
      <disk name="C">
    
        <capacity>8G</capacity>
    
        <directories>200</directories>
    
        <files>1580</files>
    
      </disk>
    
     
    
      <disk name="D">
    
        <capacity>10G</capacity>
    
        <directories>500</directories>
    
        <files>3000</files>
    
      </disk>
    
    </HD> 
    

    3.实现类

    import java.io.IOException;
    
    import java.util.List;
    
     
    
    import org.jdom.Document;
    
    import org.jdom.Element;
    
    import org.jdom.JDOMException;
    
    import org.jdom.input.SAXBuilder;
    
     
    
     
    
    public class Sample1 {
    
    public static void main(String[] args) throws JDOMException, IOException {
    
    SAXBuilder sb = new SAXBuilder();
    
    Document doc = sb.build(Sample1.class.getClassLoader().getResourceAsStream("test.xml")); //构造文档对象
    
    Element root = doc.getRootElement(); //获取根元素
    
    List list = root.getChildren("disk");//取名字为disk的所有元素
    
    for (int i = 0; i < list.size(); i++) {
    
    Element element = (Element) list.get(i);
    
    String name = element.getAttributeValue("name");
    
    String capacity = element.getChildText("capacity");//获取disk元素capacity的值
    
    String directories = element.getChildText("directories");
    
    String files = element.getChildText("files");
    
    System.out.println("磁盘信息:");
    
    System.out.println("分区盘符:" + name);
    
    System.out.println("分区容量:" + capacity);
    
    System.out.println("目录数:" + directories);
    
    System.out.println("文件数:" + files);
    
    System.out.println("------------------");
    
    }
    
    }
    
    }
    

    4.运行结果

    磁盘信息:

    分区盘符:C

    分区容量:8G

    目录数:200

    文件数:1580

    ------------------

    磁盘信息:

    分区盘符:D

    分区容量:10G

    目录数:500

    文件数:3000

    ------------------

  • 相关阅读:
    luogu_1168: 中位数
    luogu_4762: [CERC2014]Virus synthesis
    luogu_4287:双倍回文
    回文自动机学习笔记
    luogu_3645: 雅加达的摩天楼
    python爬今日头条(ajax分析)
    Python多进程multiprocessing.Pool()
    Python爬微博(ajax+mongo)
    python实用函数之join()
    python之tuple与list
  • 原文地址:https://www.cnblogs.com/flyback/p/8783961.html
Copyright © 2011-2022 走看看