1.text.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>

2.通过jdom解析xml文档
import java.util.List;

import org.jdom.*;
import org.jdom.input.SAXBuilder;


public class sample1 {
  public static void main(String[] args) throws Exception{
      SAXBuilder sb=new SAXBuilder();
     
      Document doc=sb.build(sample1.class.getClassLoader().getResourceAsStream("test.xml")); //构造文档对象
      Element root=doc.getRootElement(); //获取根元素HD
      List list=root.getChildren("disk");//取名字为disk的所有元素
      for(int i=0;i<list.size();i++){
         Element element = (Element) list.get(i);//通过集合遍历所有元素,一个disk其实就是一个元素
         String name = element.getAttributeValue("name");//获取属性为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("-----------------------------------");
      } 
    }
}

 

Face your past without regret. Handle your present with confidence.Prepare for future without fear. keep the faith and drop the fear. 面对过去无怨无悔,把握现在充满信心,备战未来无所畏惧。保持信念,克服恐惧!一点一滴的积累,一点一滴的沉淀,学技术需要不断的积淀!