Dom解析

import org.dom4j.DocumentException;

public class DomTest {
    public static void main(String[] args) {
        String file =DomTest.class.getResource("sounds.xml").toString();
        DomParser domPar = new DomParser();
        try {
            domPar.parse(file);
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
}

解析方法:

import java.util.Iterator;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class DomParser {

public void parse(String file) throws DocumentException{
    SAXReader domparser = new SAXReader();//创建解析器
    Document doc = domparser.read(file);//使用解析器解析,结果全部存入doc中
    Element root = doc.getRootElement();

    Iterator musicIterator = root.elementIterator();

    while(musicIterator.hasNext()){
        Element music = (Element)musicIterator.next();
        Iterator attrIterator = music.attributeIterator();
        while(attrIterator.hasNext()){
            Attribute arr = (Attribute)attrIterator.next();
            if("id".equals(arr.getName())){
                System.out.println("id:"+arr.getValue());
        }
    }
}
    }
}

Sound.xml

<?xml version="1.0" encoding="UTF-8"?>
<sounds>
    <music id="1">
        <name>夜来香</name>
        <singer>邓丽君</singer>
        <author>小虫</author>
        <composer>小虫</composer>
        <album>君生今世</album>
        <albumpic>images/junshengjinshi.jpg</albumpic>
        <musicpath>musics/yelaixiang.mp3</musicpath>
        <time>4:32</time>
        <downcount>1896</downcount>
        <favcount>658</favcount>
    </music>
    <music id="2">
        <name>硬币</name>
        <singer>汪峰</singer>
        <author>汪峰</author>
        <composer>汪峰</composer>
        <album>笑着哭</album>
        <albumpic>images/xiaozheku.jpg</albumpic>
        <musicpath>musics/yingbi.mp3</musicpath>
        <time>4:32</time>
        <downcount>1896</downcount>
        <favcount>658</favcount>
    </music>    
    <music id="3">
        <name>千千阙歌</name>
        <singer>陈慧娴</singer>
        <author>小虫</author>
        <composer>小虫</composer>
        <album>千千阙歌</album>
        <albumpic>images/qianqianquege.jpg</albumpic>
        <musicpath>musics/qianqianquege.mp3</musicpath>
        <time>4:32</time>
        <downcount>1896</downcount>
        <favcount>658</favcount>
    </music>    
    <music id="4">
        <name>恋曲1990</name>
        <singer>罗大佑</singer>
        <author>罗大佑</author>
        <composer>罗大佑</composer>
        <album>光阴的故事</album>
        <albumpic>images/guangyindegushi.jpg</albumpic>
        <musicpath>musics/lianqu1990.mp3</musicpath>
        <time>4:32</time>
        <downcount>1896</downcount>
        <favcount>658</favcount>
    </music>
    <music id="5">
        <name>今生今世</name>
        <singer>张国荣</singer>
        <author>小虫</author>
        <composer>小虫</composer>
        <album>I am what I am</album>
        <albumpic>images/iamwhatiam.jpg</albumpic>
        <musicpath>musics/jinshengjinshi.mp3</musicpath>
        <time>4:32</time>
        <downcount>1896</downcount>
        <favcount>658</favcount>
    </music>
    <music id="6">
        <name>蓝莲花</name>
        <singer>许巍</singer>
        <author>小虫</author>
        <composer>小虫</composer>
        <album>今天:2002-2008作品集</album>
        <albumpic>images/jintian.jpg</albumpic>
        <musicpath>musics/lanlianhua.mp3</musicpath>
        <time>4:32</time>
        <downcount>1896</downcount>
        <favcount>658</favcount>
    </music>

</sounds>

运行结果:
id:1
id:2
id:3
id:4
id:5
id:6

你可能感兴趣的:(java,xml)