---
title: "Java 读取xml文件"
author: "feiyu86"
date: "2011-03-22T09:23:44Z"
tags: []
categories: ["企业架构"]
slug: "java-xml"
draft: false
lastmod: "2011-03-22T09:26:35Z"
id: 970122
url: "https://feiyu86.iteye.com/blog/970122"
---

```
URL initfile = this.getClass().getClassLoader().getResource(
					"messageHandler.xml");				
				SAXBuilder builder = new SAXBuilder();
				 Document doc = builder.build(new File(initfile.getPath()));
				// 获取channel列表
				List channels = doc.getRootElement().getChildren("channel");
				// 获取delay配置
				Element delayElm = doc.getRootElement().getChild("delay");
				// 如果配置了delay参数
				if(delayElm != null)
					// 设置delay
					delay = Long.parseLong(delayElm.getAttributeValue("value"));
				// 如果配置了channel信息
				if(channels != null && channels.size()>0){
					// 对于每个channel信息
					for (int i = 0; i < channels.size(); i++) {
						// 获取对应的action配置和通道信息（value值）
						Element elm = (Element) channels.get(i);
						List actions = elm.getChildren("action");
						String key = elm.getAttributeValue("value");
```

```
	XML文件结构
<channel value="">
		<action>
			<id></id>
			<bindclass></bindclass>
		</action>
	</channel>
```