新手询问关于jdom的问题。着急中,在线等待
找了一个例子,在jbulider7下,编译,结果总是提示我“Hit uncaught exception java.lang.ClassCastException”。这是怎么回事亚。
万分着急中。………………………………………………
我的代码如下:
package com.lim.xmlparse;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.util.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class XMLToolkit {
public XMLToolkit() {
}
public static void main(String args[]){
SAXBuilder sb = new SAXBuilder();
//创建文档
Document doc = null;
try {
doc = sb.build(new FileInputStream("e:/program/haierwms/src/conf/exampleA.xml"));
}
catch (Exception ex) {
ex.printStackTrace();
}
//加入一条处理指令
ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\"");
doc.addContent(pi);
//得到根节
Element root = doc.getRootElement();
//得到根节点所有子元素的集合
List books = root.getChildren();
//得到第一个book元素
Element book = (Element)books.get(0);
//添加一条属性
Attribute a = new Attribute("hot","true");
book.addAttribute(a);
book.setAttributes(books);
//得到指定的字元素
Element author = book.getChild("author");
author.setText("王五"); //将作者改为王五
//或 Text t = new Text("王五");book.addContent(t);
//得到指定的字元素
Element price = book.getChild("price");
author.setText(Float.toString(50.0f));
String indent = " ";
boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");
try {
outp.output(doc, new FileOutputStream("exampleB.xml"));
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}