JDOM程序无法运行
用JDOM写了一个简单的读取和修改XML文档的类文件,编译都没有问题,运行时出错。
错误信息如下:
Exception in thread "main" java.lang.NoClassDefFoundError: TestJDOM
源文件如下:
//testJDOM.java
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
public class TestJDOM{
public static void main(String args[])throws Exception{
SAXBuilder sb = new SAXBuilder();
//从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了
Document doc = sb.build(new FileInputStream("exampleA.xml"));
//加入一条处理指令
ProcessingInstruction pi = new ProcessingInstruction
("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\"");
doc.addContent(pi);
Element root = doc.getRootElement(); //得到根元素
java.util.List books = root.getChildren(); //得到根元素所有子元素的集合
Element book = (Element)books.get(0); //得到第一个book元素
//为第一本书添加一条属性
Attribute a = new Attribute("hot","true");
book.setAttribute(a);
Element author = book.getChild("author"); //得到指定的字元素
author.setText("王五"); //将作者改为王五
//或 Text t = new Text("王五");book.addContent(t);
Element price = book.getChild("price"); //得到指定的字元素
//修改价格,比较郁闷的是我们必须自己转换数据类型,而这正是JAXB的优势
author.setText(Float.toString(50.0f));
String indent = " ";
boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");
outp.output(doc, new FileOutputStream("exampleB.xml"));
}
};
另外一个程序编译通过,运行时也是同样的错误信息