我想实现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");
结果还是报一样的错,郁闷了!

解决方案 »

  1.   

    貌似解决了,换了种写法:
    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
        }
    }
    大家还有更好的方法么?