Home > Java > javaTutorial > body text

How to read XML files using Jdom

高洛峰
Release: 2017-03-12 09:21:01
Original
1708 people have browsed it

Use Jdom to read XML file method. When learning Spring, we often see many xmlconfiguration files. Spring uses IOC (Control Response) through the configuration in the configuration file. (Transfer), so as to achieve the flexibility of the code. In this article, I will introduce to you a way to parse xml--Jdom

First we go to the Jdom website to download the corresponding JAR package file and import it In our java project, we then write an xml file:

<?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>
Copy after login

Next we can use Jdom to read the configuration information in xml. Our java class is as follows:

public class Jdom {
    
    public static void main(String[] args) {
        try {
            readXml();
        } catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void readXml() throws JDOMException, IOException{
        SAXBuilder saxBuilder = new SAXBuilder();
        Document doc = saxBuilder.build(Jdom.class.getClassLoader().getResourceAsStream("test.xml"));
        Element root = doc.getRootElement();//获得xml的根元素
        List<Element> list = root.getChildren("disk");//获得根元素下的子孩子
        for(Element element : list){
            String name = element.getAttributeValue("name");//取disk元素的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("-----------------------------------");
        }
    }

}
Copy after login

There must be many ways to read XML. This is just to let everyone know. If you have a better way or method, I hope you can teach me. Thanks

The above is the detailed content of How to read XML files using Jdom. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template