新手询问关于jdom的问题。着急中,在线等待

luckgirl 2003-04-15 11:13:51
找了一个例子,在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();
}
}

}
...全文
107 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
heixiu1980 2003-04-25
  • 打赏
  • 举报
回复
顶一下,我也有这样的问题。
newman0708 2003-04-25
  • 打赏
  • 举报
回复
Attribute a = new Attribute("hot","true");
book.addAttribute(a);

Element author = book.getChild("author"); //得到指定的字元素
System.out.println("author: "+author.getText());
author.setText("王五"); //将作者改为王五

Element price = book.getChild("price"); //得到指定的字元素
price.setText(Float.toString(50.0f));

我做过了,应该没有问题的.
thinkerhj 2003-04-25
  • 打赏
  • 举报
回复
1.
//添加一条属性
Attribute a = new Attribute("hot","true");
book.addAttribute(a);
book.setAttributes(books);
改成
Attribute a = new Attribute("hot", "true");
book.setAttribute(a);
2.
//得到指定的字元素
Element price = book.getChild("price");
author.setText(Float.toString(50.0f));
改成
Element price = book.getChild("price");
price.setText(Float.toString(50.0f));

这样可以运行了。


heixiu1980 2003-04-25
  • 打赏
  • 举报
回复
没用,我试过的
高版本的JDom把AddAttributes改成了setAttributes
wks9527 2003-04-25
  • 打赏
  • 举报
回复
book.setAttributes(books);
这一句有点奇怪,去掉试试看。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧