<e v=5,l=6>4</e> -----><e v="5" l="6">4</e>

解决方案 »

  1.   

    小弟也正在学习,以下是我写的,测试过了,希望大家共同学习!
    package jdomdemo;
    import org.jdom.*; 
    import org.jdom.output.*; 
    import java.io.*;
    public class TestJDom3 { public static void main(String[] args) {

    Element root=new Element("rootm");
    Document myDocument=new Document(root);


    Element a = new Element("a");
    a.addContent("1");
    Element b = new Element("b");
    b.addContent("2");

    Element c = new Element("c");
    Element d = new Element("d");
    d.addContent("3");
    c.addContent(d);

    Element e = new Element("e");
    e.setAttribute("v","5");
    e.setAttribute("l","6");
    e.addContent("4");

    Element f = new Element("f");
    Element g = new Element("g");
    g.setAttribute("t","7");
    g.addContent("5");
    f.addContent(g);

    root.addContent(a);
    root.addContent(b);
    root.addContent(c);
    root.addContent(e);
    root.addContent(f);

    try {
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    outputter.output(myDocument, System.out);

    outputter.output(myDocument, new FileOutputStream("myFile.xml"));
    } catch (java.io.IOException ee) {
    ee.printStackTrace();

    }
    }
    }
      

  2.   

    XMLOutputter是不是要什么包啊,我这编译不过,我用的是JC编辑器.
      

  3.   

    import org.jdom.output.XMLOutputter有。可就是报错。好象是FORMAT的问题
      

  4.   

    Format.getPrettyFormat()去掉就对了,这是干什么的。
      

  5.   

    static Format getPrettyFormat()           Returns a new Format object that performs whitespace beautification with 2-space indents, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy. 主要是用来格式化输出的
      

  6.   

    读XML出来的数据都认为是字符型的
      

  7.   

    为什么加Format.getPrettyFormat()就不对了,另外怎么在这个文件里面换行,现在都是一行的.
      

  8.   

    请问冬瓜,你的程序是在jdk多少下编译的,Format.getPrettyFormat()中Format是哪个变量?
    你的setAttribute有这个函数:setAttribute(String,String);
    你的jdom包是多少版本的
      

  9.   

    自己来回答吧!刚刚查到的。如果用jdom 9以上就没有问题。
    Format是一个Jdom中的类,Format.getPrettyFormat()就可以完成换行。
      

  10.   

    参照http://dev.csdn.net/article/20/20721.shtm
    的方法,读就是了。跟写差别不大