解析xml文件。
//import javax.xml.parsers.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
import java.io.*;
import org.xml.sax.*;
/**
*使用DOM提取XML内容的例子
*/
public class BookParser{
public static void main(String[] args){
try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File("books.xml"));
document.normalize();
Element root=document.getDocumentElement();
System.out.println("根原始的名称:"+root.getTagName());
NodeList books=root.getElementsByTagName("book");
System.out.println("书本列表");
for(int i=0;i<books.getLength();i++)
{
Element book=(Element)books.item(i);
String book_id=book.getAttribute("id");//或book.getAttributeNode("id").getValue();
System.out.println("序号:"+book_id);
System.out.print("作者:");
System.out.println(book.getElementsByTagName("author").item(0).getFirstChild().getNodeValue());
System.out.print("标题:");
System.out.println(book.getElementsByTagName("title").item(0).getFirstChild().getNodeValue());
Element publishDate=(Element)book.getElementsByTagName("publish_date").item(0);
String year=publishDate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue();
String month=publishDate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();
String day=publishDate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();
System.out.println("出版日期:"+year+"年"+month+"月"+day+"日");
System.out.print("描述:");
System.out.println(book.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
System.out.println("--------------------");
}
}
catch(Exception e2)
{
System.out.println("asdf");
}
}
}
总报错java.lang.noclassdeffounderror,环境是jdk1.4,
问题点数:20、回复次数:5Top
1 楼wangweitao1979(末日魔鬼)回复于 2002-09-30 17:34:58 得分 10
看看你所引入的类里面有没有org.apache.xerces这个包,这个包里面有DocumentBuilder的实现类,Top
2 楼fristykily(firstykily)回复于 2002-10-14 17:31:03 得分 0
极其关注Top
3 楼chenyuan_tongji(codeguru)回复于 2002-10-14 17:51:32 得分 0
没有加载xerces包
http://xml.apache.org/xerces2-j/index.htmlTop
4 楼littlecong(虫子)回复于 2002-10-14 19:48:21 得分 10
jaxp是需要具体的parser的,所以必须作相应的配置Top
5 楼dauglus(快乐鸟)回复于 2002-10-14 21:21:20 得分 0
agree with chenyuan_tongji
maybe the class path setting in ur command line is incorrect, u must point out the path to xerces.Top




