DocumentBuilderFactory

DocumentBuilderFactory

我需要从rss提要中每5分钟更新一次新闻提要。

为此,我编写了一个TimerTask,如下所示

public class TimerTaskForAllNews
{
    public static void main( String[] args )
    {
        TimerTask task = new AllNewsUpdatrUtility();
        Timer timer = new Timer();
        timer.schedule(task, 1000,60000);
    }
}


这是我的TimerTask实现类

package com.util;
import java.net.URL;
public class AllNewsUpdatrUtility extends TimerTask {
      private static AllNewsUpdatrUtility instance = null;
       public AllNewsUpdatrUtility() {}
       public static AllNewsUpdatrUtility getInstance() {
          if (instance == null)
             instance = new AllNewsUpdatrUtility();
          return instance;
       }
    @Override
    public void run() {
         try {
             JSONArray latestnews = new JSONArray();
             JSONObject jsonobj_allnews = new JSONObject();
             DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
             URL url = new URL("http://www.rssmix.com/u/8160628/rss.xml");
             Document doc = builder.parse(url.openStream());
             NodeList items = doc.getElementsByTagName("item");
            for (int i = 0; i < items.getLength(); i++) {
                Element item = (Element) items.item(i);
                String title = getValue(item, "title");
                String link = getValue(item, "link");
                String pub_date = getValue(item, "pubDate");

            } // for loop ends here

          } catch (Exception e) {
             e.printStackTrace();
          }
    }


}


您能否让我知道我仍然可以改进此程序?

最佳答案

规范JSR 206 Java™ API for XML Processing (JAXP) 1.4


  可以预期SAXParserFactory实现的newSAXParser方法,DocumentBuilderFactory的newDocumentBuilder方法和TransformerFactory的newTransformer方法将是线程安全的,并且没有副作用。


如评论中所述,您可以缓存DocumentBuilderFactory实例:

package com.util;
import java.net.URL;
public class AllNewsUpdatrUtility extends TimerTask {
       private static AllNewsUpdatrUtility instance;
       private final DocumentBuilderFactory dbf;
       private AllNewsUpdatrUtility() {}
       public synchronized static AllNewsUpdatrUtility getInstance() {
          if (instance == null)
             instance = new AllNewsUpdatrUtility();
             dbf = DocumentBuilderFactory.newInstance();
          return instance;
       }
    @Override
    public void run() {
         try {
             JSONArray latestnews = new JSONArray();
             JSONObject jsonobj_allnews = new JSONObject();
             DocumentBuilder builder = dbf.newDocumentBuilder();
             URL url = new URL("http://www.rssmix.com/u/8160628/rss.xml");
             Document doc = builder.parse(url.openStream());
             NodeList items = doc.getElementsByTagName("item");
            for (int i = 0; i < items.getLength(); i++) {
                Element item = (Element) items.item(i);
                String title = getValue(item, "title");
                String link = getValue(item, "link");
                String pub_date = getValue(item, "pubDate");

            } // for loop ends here

          } catch (Exception e) {
             e.printStackTrace();
          }
    }


}

10-01 22:10