【原创】使用DOM解析XML文档示例(Java)

待解析的XML文档如下:

1 <?xml version="1.0" encoding="UTF-8"?>
2  <collection>
3 <cd>
4 <title>Boys for Pele</title>
5 <artist>Tori Amos</artist>
6 <tracks>
7 <track type="vocal">
8 <name>Horses</name>
9 <length>3.5</length>
10 </track>
11 <track type="instrumental">
12 <name>Blood roses</name>
13 <length>3.2</length>
14 </track>
15 <track type="vocal">
16 <name>Father lucifer</name>
17 <length>3.8</length>
18 </track>
19 <track type="instrumental">
20 <name>Professional widow</name>
21 <length>4.1</length>
22 </track>
23 <track type="vocal">
24 <name>Mr. Zebra</name>
25 <length>3.6</length>
26 </track>
27 </tracks>
28 </cd>
29 <cd>
30 <title>The Ghosts that Hunt me</title>
31 <artist>Crash TestDummies</artist>
32 <tracks>
33 <track type="vocal">
34 <name>Winter song</name>
35 <length>4.3</length>
36 </track>
37 <track type="instrumental">
38 <name>Comin's back soon</name>
39 <length>4.1</length>
40 </track>
41 <track type="vocal">
42 <name>Superman's song</name>
43 <length>4.1</length>
44 </track>
45 <track type="vocal">
46 <name>Here on earth</name>
47 <length>3.1</length>
48 </track>
49 </tracks>
50 </cd>
51  </collection>

 

DOM解析程序如下:

1 import org.w3c.dom.*;
2  import javax.xml.parsers.*;
3  import java.io.*;
4
5  public class TestDOM
6 {
7 public static void main(String agrs[])
8 {
9 try{
10 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
11 DocumentBuilder builder=factory.newDocumentBuilder();
12 Document document=builder.parse(new File("C04Ex1.xml"));
13
14 System.out.println("\n=======================================================\n");
15
16 String version=document.getXmlVersion();
17 System.out.println("XML声明的版本号: "+version);
18
19 String encoding=document.getXmlEncoding();
20 System.out.println("XML声明的编码: "+encoding);
21
22 Element root=document.getDocumentElement();
23 String rootName=root.getNodeName();
24 System.out.println("XML文件根结点的名字: "+rootName);
25
26 System.out.println("\n=======================================================\n");
27
28 NodeList nl_cd=document.getElementsByTagName("cd");
29 int size_cd=nl_cd.getLength();
30
31 for(int k=0; k<size_cd; k++)
32 {
33 Element elt_cd=(Element)nl_cd.item(k);
34
35 Node title=elt_cd.getElementsByTagName("title").item(0);
36 String strTitle=title.getFirstChild().getNodeValue();
37 System.out.println("CD标题为: "+strTitle);
38
39 Node artist=elt_cd.getElementsByTagName("artist").item(0);
40 String strArtist=artist.getFirstChild().getNodeValue();
41 System.out.println("艺术家名为: "+strArtist);
42
43 Element elt_tracks=(Element)elt_cd.getElementsByTagName("tracks").item(0);
44 NodeList nl_track=elt_tracks.getElementsByTagName("track");
45
46 int size_track=nl_track.getLength();
47 System.out.println("此CD共有歌曲数为: "+size_track);
48
49 for(int i=0; i<size_track; i++)
50 {
51 Element elt_track=(Element)nl_track.item(i);
52
53 Node trackName=elt_track.getElementsByTagName("name").item(0);
54 String strName=trackName.getFirstChild().getNodeValue();
55
56 Node trackLength=elt_track.getElementsByTagName("length").item(0);
57 String strLength=trackLength.getFirstChild().getNodeValue();
58
59 Attr trackType=elt_track.getAttributeNode("type");
60 String strType=trackType.getFirstChild().getNodeValue();
61
62 System.out.println("歌名: "+strName+"\t\t时长: "+strLength+"\t类型: "+strType);
63 }
64 System.out.println("\n-------------------------------------------------------\n");
65 }
66 }
67 catch(Exception e)
68 {
69 System.out.println(e);
70 }
71 }
72 }
73  

 

程序运行的效果如下:

 

posted @ 2010-06-25 19:22  it-mac  阅读(744)  评论(0编辑  收藏  举报