2013-04-30 101 views
0

我想在我的項目中添加jdom.jar,我做了project-> properties-> Java Build Path-> libraries->添加外部jar jdom-2.0.5,但導入沒有考慮。我如何提供jdom.jar。這是我想測試的示例在我的java項目中添加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

我相信你正在使用Eclipse作爲IDE,對不對? – 2013-04-30 10:03:10

+1

爲什麼不被「考慮」?它是否顯示在構建路徑概述中? – Thilo 2013-04-30 10:03:39

+0

@STT LCU右我使用eclipse – user2232659 2013-04-30 10:30:51

回答

2

JDOM 2.0.5使用與示例代碼不同的API(略)。由於一些項目需要原始的JDOM(沒有泛型)和新的JDOM與泛型,決定將JDOM包重命名爲org.jdom2。*

在幾乎所有情況下,它都只是改變你的從進口org.jdom.xxxxx進口org.jdom2.xxxx

看到這個https://github.com/hunterhacker/jdom/wiki/JDOM2-Migration-Issues

+0

感謝您的幫助,我的問題已解決:)! – user2232659 2013-04-30 10:50:10

+0

不客氣,盡情享受JDOM ;-) – rolfl 2013-04-30 11:05:00

+0

@ user2232659如果問題得到解決,請將此答案標記爲已接受。 – 2013-04-30 11:40:02