Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在我的java项目中添加jdom.jar_Java_Jdom - Fatal编程技术网

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

在我的java项目中添加jdom.jar,java,jdom,Java,Jdom,我想在我的项目中添加jdom.jar,我做了项目->属性->Java构建路径->库->添加外部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.o

我想在我的项目中添加jdom.jar,我做了项目->属性->Java构建路径->库->添加外部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());
  }
}
   }

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

在几乎所有情况下,只需将导入从import org.jdom.xxxxx更改为org.jdom2.xxxx即可


看到这个了吗

我想你正在使用eclipse作为你的IDE,对吧?为什么不“考虑”呢?它是否出现在生成路径概述中?@STT LCU对,我正在使用eclipse@Thilo它出现在构建路径中,但无法解析导入org.jdom.*!!在教程中,他说:“然后将文件/build/jdom.jar放在类路径中,使其可用”