---
title: "JDOM解析XML"
author: "HeLinHang"
date: "2011-08-16T21:32:50Z"
tags: []
categories: ["编程语言"]
slug: "jdom-xml"
draft: false
lastmod: "2011-08-16T21:32:50Z"
id: 1150806
url: "https://ibook.iteye.com/blog/1150806"
---

待解析的XML

```
<?xml version="1.0" encoding="utf-8"?>
<musices>
	<music>
		<name>Hello</name>
		<size>8622</size>
	</music>
	<music>
		<name>World</name>
		<size>300</size>
	</music>
</musices>	
```

 

```
	SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(new File("E:\\music.xml"));
		Element foo =  doc.getRootElement();
		List allChildren = foo.getChildren();
		for(int i=0;i<allChildren.size();i++) {
		System.out.print("歌曲名称:" + ((Element)allChildren.get(i)).getChild("name").getText());
		System.out.println("歌曲大小:" + ((Element)allChildren.get(i)).getChild("size").getText());}
```

 同时导入jdom.jar