这两天在练习Struts1.2+Hibernate3.1在MyEclipse5.1下的编程。
Struts及Hibernate这一年来断断续续地基本语法及核心技术也熟悉了不少,就找了个RSS在线阅读的实例练手。
中间就遇到了一个对RSS XML文档进行解析的问题。
核心解析代码如下:
public static List parseFeed(String address) {
//System.setProperty( "javax.xml.parsers.DocumentBuilderFactory","com.sun.org.apache.crimson.jaxp.DocumentBuilderFactoryImpl" );
//
List<Object> result = new ArrayList<Object>();
//
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
System.out.println("factory:" + dbf);
//
DocumentBuilder db;
Document doc;
try {
//
db = dbf.newDocumentBuilder();
//
//doc = db.parse(address);
java.io.File file = new java.io.File(address);
doc = db.parse(file);
//
NodeList nL = doc.getElementsByTagName("item");
//
for (int i = 0; i < nL.getLength(); i++) {
Node node = nL.item(i);
NodeList nL2 = node.getChildNodes();
Node nodeTitle = nL2.item(1);
Node nodeAuthor = nL2.item(2);
Node nodeUrl = nL2.item(3);
Node nodeDescription = nL2.item(4);
String title = nodeTitle.getFirstChild().getNodeValue();
String author = nodeAuthor.getFirstChild().getNodeValue();
String url = nodeUrl.getFirstChild().getNodeValue();
String description = nodeDescription.getFirstChild().getNodeValue();
//通过得到的信息构建Item对象
//Item item = new Item(title,description,author,url);
//将生成的item放到result List列表中,以便返回
//result.add(item);
}
}
catch (ParserConfigurationException e) {
//
e.printStackTrace();
}
catch (SAXException e) {
//
e.printStackTrace();
}
catch (IOException e) {
//
e.printStackTrace();
} finally {
//
//System.setProperty( "javax.xml.parsers.DocumentBuilderFactory","com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl" );
return result;
}
//return result;
}
但结果却是返回NULL空值。我查看了一直这一语句:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
中dbf的值为:
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl@8f4fb3
在elseif的专栏中有专门讨论这个问题的(URL:http://blog.youkuaiyun.com/elseif/archive/2005/04/29/367506.aspx)
但可惜的是我用的JDK1.5.0_06没有包 org.apache.crimson.jaxp.DocumentBuilderFactoryImpl(在我那如果有完整路径应该是com.sun.org.apache.crimson.jaxp.DocumentBuilderFactoryImpl),但是确实没有这样的类。因为根本找不到com.sun.org.apache.crimson.*这个包,我想是不是我的JDK的问题,正在寻找解决之道。。。。