Java JDOM读取XML

JDOM是一个开源项目,它基于树型结构,利用JAVA的技术对XML文档实现解析、生成、序列化以及多种操作,用Java的数据类型来定义操作数据树的各个节点。

一、JDOM下载

jdom-2.0.4.jar下载: http://www.jdom.org/downloads/

二、JDOM解析XML文件

XML文件如下:

<?xml version="1.0" encoding="UTF-8"?>      
<AirPortLine>      
    <line lid="MU2480" num="3">      
        <id>1</id>      
        <station>      
            <sid>1</sid>      
            <sname>武汉</sname>      
        </station>      
        <station>      
            <sid>2</sid>      
            <sname>石家庄</sname>      
        </station>      
        <station>      
            <sid>3</sid>      
            <sname>北京</sname>      
        </station>      
    </line>      
    <line lid="MU2483" num="4">      
        <id>2</id>      
        <station>      
            <sid>1</sid>      
            <sname>重庆</sname>      
        </station>      
        <station>      
            <sid>2</sid>      
            <sname>武汉</sname>      
        </station>      
        <station>      
            <sid>3</sid>      
            <sname>上海</sname>      
        </station>      
        <station>      
            <sid>4</sid>      
            <sname>东京</sname>      
        </station>      
    </line>      
    <line lid="MU2485" num="2">      
        <id>3</id>      
        <station>      
            <sid>1</sid>      
            <sname>上海</sname>      
        </station>      
        <station>      
            <sid>2</sid>      
            <sname>纽约</sname>      
        </station>      
    </line>      
</AirPortLine>
JDOM解析XML代码如下:
package com.hsinghsu.testXML;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class testJDOM {

	public static void main(String[] args) {

		try {
			File file = new File("AirPortLine.xml");
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(file);

			parseJDOM(doc);// 解析XML文档

		} catch (Exception e) {
			System.out.println("Can't read the file");
		}
	}

	// 解析XML文档
	private static void parseJDOM(Document doc) {
		Element root = doc.getRootElement();
		List lineList = root.getChildren("line");// 也可使用root.getChildren()
		for (Iterator iter = lineList.iterator(); iter.hasNext();) {
			Element lineElement = (Element) iter.next();// 获取<line>元素

			String lid = lineElement.getAttributeValue("lid");// 获取<line>元素的lid属性值
			String num = lineElement.getAttributeValue("num");// 获取<line>元素的num属性值

			System.out.println("==lid:" + lid);
			System.out.println("==num:" + num);

			Element idElement = lineElement.getChild("id");// 获得<line>下<id>标签下的子元素
			String id = idElement.getText();// // 获得<line>下<id>标签下的子元素值
			System.out.println("==路线id:" + id);

			List stationList = lineElement.getChildren("station");// 获得<line>下<station>列表
			for (Iterator subIter = stationList.iterator(); subIter.hasNext();) {
				Element stationElement = (Element) subIter.next();// 获取<station>元素

				Element sidElement = stationElement.getChild("sid");// 获得<station>下<sid>标签下的子元素
				Element snameElement = stationElement.getChild("sname");// 获得<station>下<sname>标签下的子元素
				String sid = sidElement.getText();// 获得<station>下<sid>标签下的子元素值
				String sname = snameElement.getText();// 获得<station>下<sname>标签下的子元素值

				System.out.println("==路线sid:" + sid);
				System.out.println("==路线sname:" + sname);
			}

		}
	}

}

你可能感兴趣的:(Java JDOM读取XML)