Java中XML文档的解析问题(已解决)

在使用Struts1.2和Hibernate3.1进行RSS在线阅读应用的编程时,作者遇到了XML文档解析的问题。尝试使用DocumentBuilderFactory解析XML文件,但返回结果为空。经过检查,发现JDK1.5.0_06中缺少com.sun.org.apache.crimson.jaxp.DocumentBuilderFactoryImpl类。作者查阅了相关资料,但未找到对应的解决方案,目前仍在寻求解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这两天在练习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的问题,正在寻找解决之道。。。。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值