import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;

public class XMLDisplay {
public static void  main(String args[]){
  try{
  	// 为解析XML文件创建DOM对象
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder=factory.newDocumentBuilder();
    Document doc=builder.parse(new File("mylinks.xml"));
    // 初始化
    doc.normalize();
    // 得到节点集合
    NodeList links =doc.getElementsByTagName("link");
    // 通过For循环遍历
    for (int i=0;i<links.getLength();i++) {
      // 得到当前元素	
      Element link= (Element)links.item(i);
      System.out.print("Content: ");    System.out.println(link.getElementsByTagName("text").item(0).getFirstChild().getNodeValue()); 
      System.out.print("URL: ");
      System.out.println(link.getElementsByTagName("url").item(0).getFirstChild().getNodeValue());
      System.out.print("Author:"); System.out.println(link.getElementsByTagName("author").item(0).getFirstChild().getNodeValue());
      System.out.print("Date: ");
      Element linkdate=(Element) link.getElementsByTagName("date").item(0);
      String day=linkdate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();
      String month=linkdate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();
      String year=linkdate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue();
      System.out.println(day+"-"+month+"-"+year);
      System.out.print("Description:");    System.out.println(link.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
      System.out.println();
    }
  }catch(Exception e){
    e.printStackTrace();
  }
}
}
