java解析xml文件

导入jdom 包。

一: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> 

 

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class Test {
    
    public static void main(String[] args) throws Exception{   
        SAXBuilder sb=new SAXBuilder();  
        
//        InputStream in =   Test.class.getClassLoader().getResourceAsStream("D:\\test.xml");
        File file = new File("D:\\test.xml");
        InputStream in = new FileInputStream(file);
        Document doc=sb.build(in);
        
        
//        Document doc=sb.build(Test.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);  
           String name = element.getAttributeValue("name");//获取name属性值  
           String capacity=element.getChildText("capacity");//取disk子元素capacity的内容  
           String directories=element.getChildText("directories");//取disk子元素directories的内容  
           String files=element.getChildText("files");//取disk子元素files的内容  
           System.out.println("磁盘信息:");  
           System.out.println("分区盘符:"+name);  
           System.out.println("分区容量:"+capacity);  
           System.out.println("目录数:"+directories);  
           System.out.println("文件数:"+files);  
           System.out.println("-----------------------------------");  
             
        }    
      }  

}

输出:

磁盘信息:
分区盘符:C
分区容量:8G
目录数:200
文件数:1580
-----------------------------------
磁盘信息:
分区盘符:D
分区容量:10G
目录数:500
文件数:3000
-----------------------------------

 

二:解析传入的xml格式的字符串:

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class Test {
    
    public static void main(String[] args) throws Exception{   
        SAXBuilder sb=new SAXBuilder();  
        
//        InputStream in =   Test.class.getClassLoader().getResourceAsStream("D:\\test.xml");
//        File file = new File("D:\\test.xml");
//        InputStream in = new FileInputStream(file);
//        Document doc=sb.build(in);
        
        String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><HD><disk name=\"C\"><capacity>8G</capacity>  <directories>200</directories>  <files>1580</files></disk>  </HD> ";
        InputStream   in   =   new   ByteArrayInputStream(str.getBytes());   
        InputStream   in2   =   new   ByteArrayInputStream(str.getBytes("UTF-8"));  
        Document doc=sb.build(in2);
        
//        Document doc=sb.build(Test.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);  
           String name = element.getAttributeValue("name");//获取name属性值  
           String capacity=element.getChildText("capacity");//取disk子元素capacity的内容  
           String directories=element.getChildText("directories");//取disk子元素directories的内容  
           String files=element.getChildText("files");//取disk子元素files的内容  
           System.out.println("磁盘信息:");  
           System.out.println("分区盘符:"+name);  
           System.out.println("分区容量:"+capacity);  
           System.out.println("目录数:"+directories);  
           System.out.println("文件数:"+files);  
           System.out.println("-----------------------------------");  

        }   
        
      }      
}

 

posted @ 2018-03-24 23:44  zhoudingzhao  阅读(199)  评论(0编辑  收藏  举报