在我的java项目中添加jdom.jar

时间:2013-04-30 10:02:07

标签: java jdom

我想在我的项目中添加jdom.jar,我做项目 - >属性 - > Java Build Path-> libraries->添加外部jar jdom-2.0.5,但不考虑导入。我怎样才能提供jdom.jar。这是我想测试的例子

 import java.io.FileWriter;
 import java.io.IOException;
 import org.jdom.Attribute;
 import org.jdom.Document;
 import org.jdom.Element;
 import org.jdom.output.Format;
 import org.jdom.output.XMLOutputter;

  public class WriteXMLFile {
    public static void main(String[] args) {

       try {

    Element company = new Element("company");
    Document doc = new Document(company);
    doc.setRootElement(company);

    Element staff = new Element("staff");
    staff.setAttribute(new Attribute("id", "1"));
    staff.addContent(new Element("firstname").setText("yong"));
    staff.addContent(new Element("lastname").setText("mook kim"));
    staff.addContent(new Element("nickname").setText("mkyong"));
    staff.addContent(new Element("salary").setText("199999"));

    doc.getRootElement().addContent(staff);

    Element staff2 = new Element("staff");
    staff2.setAttribute(new Attribute("id", "2"));
    staff2.addContent(new Element("firstname").setText("low"));
    staff2.addContent(new Element("lastname").setText("yin fong"));
    staff2.addContent(new Element("nickname").setText("fong fong"));
    staff2.addContent(new Element("salary").setText("188888"));

    doc.getRootElement().addContent(staff2);

    // new XMLOutputter().output(doc, System.out);
    XMLOutputter xmlOutput = new XMLOutputter();

    // display nice nice
    xmlOutput.setFormat(Format.getPrettyFormat());
    xmlOutput.output(doc, new FileWriter("c:\\file.xml"));

    System.out.println("File Saved!");
  } catch (IOException io) {
    System.out.println(io.getMessage());
  }
}
   }

1 个答案:

答案 0 :(得分:2)

JDOM 2.0.5使用与示例代码不同的API(略)。因为有些项目需要原始JDOM(没有Generics)和带有Generics的新JDOM,所以决定将JDOM包重命名为org.jdom2。*

几乎在所有情况下,只需将导入从org.jdom.xxxxx导入到org.jdom2.xxxx

请参阅此https://github.com/hunterhacker/jdom/wiki/JDOM2-Migration-Issues