faint!XMLOutputer总报错!

yzb85 2008-09-30 06:10:47
我想实现JAVA对XML的改写,我看这篇教材很多地方都用到了:
http://www.knowsky.com/364082.html

于是我按步就搬,但在写到:
XMLOutputter outputter = new XMLOutputter(" ",true,"gb2312");
时报错:
cannot find symbol; symbol : constructor XMLOutputter(java.lang.String,boolean,java.lang.String), location: class org.jdom.output.XMLOutputter at line 33 (33:31)
我的已经在前面导了包啊:
import org.jdom.output.XMLOutputter;
还有人说这样写也可以:
org.jdom.output.XMLOutputter outp = new org.jdom.output.XMLOutputter("",true,"gb2312");
结果还是报一样的错,郁闷了!
...全文
70 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzb85 2008-09-30
  • 打赏
  • 举报
回复
貌似解决了,换了种写法:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;



public class xmlwrite {
public xmlwrite() { }
public void xmlwriteplayer(units player[],String file) throws Exception{
Element root=new Element("gamesave");
Document doc=new Document(root);

//加入子元素:
Element name=new Element("player");
name.setAttribute(new Attribute("id","111"));
root.addContent(name);


//因为addContent()方法返回值为Element类型,上面的代码也可以写成:
root.addContent(new Element("姓名").addContent("张二").setAttribute("ID号","2222"));
root.addContent(new Element("年龄").addContent("20"));

// 使用FileOutputStream,生成XML文本
try
{
//String ident=" "; //子元素缩进两个空格
//Boolean isNew=true; //元素间有空行
//String cset="utf-8"; //编码,显示中文
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat().setEncoding("utf-8"));


outputter.output(doc,new FileOutputStream("1.xml"));
}catch(IOException e)
{
e.printStackTrace();
}

//资料引用:http://www.knowsky.com/364082.html




}
}
大家还有更好的方法么?

62,616

社区成员

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

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