1. package util;  
  2.  
  3. import java.util.Vector;  
  4.  
  5. import module.News;  
  6.  
  7. import org.w3c.dom.Document;  
  8. import org.w3c.dom.Element;  
  9. import org.w3c.dom.Node;  
  10. import org.w3c.dom.NodeList;  
  11.  
  12. public class ParseXml {  
  13.     Vector n = new Vector();  
  14.  
  15.     public Vector parseXml(Document d) {  
  16.         Element rootElement = d.getDocumentElement();  
  17.         rootElement.normalize();  
  18.         displayNode(rootElement, 0);  
  19.         return n;  
  20.     }  
  21.  
  22.     private void displayNode(Node node, int depth) {  
  23.  
  24.         if (node.getNodeType() == Node.ELEMENT_NODE) {  
  25.             NodeList childNodes = node.getChildNodes();  
  26.             int numChildren = childNodes.getLength();  
  27.  
  28.             if (node.getNodeName().equals("item")) {  
  29.                 NodeList child = node.getChildNodes();  
  30.                 int a = child.getLength();  
  31.                 String title = "";  
  32.                 String body = "";  
  33.                 String time = "";  
  34.                 for (int j = 0; j < a; j++) {  
  35.                     String s;  
  36.                     String tagname = null;  
  37.                     try {  
  38.                         tagname = child.item(j).getNodeName();  
  39.                         s = child.item(j).getFirstChild().getNodeValue();  
  40.                     } catch (Exception e) {  
  41.                         s = "";  
  42.                     }  
  43.                     if (tagname.equals("title")) {  
  44.                         title = s;  
  45.                         continue;  
  46.                     }  
  47.                     if (tagname.equals("description")) {  
  48.                         body = s;  
  49.                         continue;  
  50.                     }  
  51.                     if (tagname.equals("pubDate")) {  
  52.                         time = s;  
  53.                         continue;  
  54.                     }  
  55.  
  56.                 }  
  57.  
  58.                 News news = new News(title, time, body);  
  59.                 n.addElement(news);//保存到vector中  
  60.             } else {  
  61.  
  62.                 for (int i = 0; i < numChildren; ++i) {  
  63.                     displayNode(childNodes.item(i), depth + 1);  
  64.                 }  
  65.             }  
  66.  
  67.         }  
  68.     }  
  69.  
  70. }