user2232659
user2232659

Reputation: 1

add jdom.jar in my java project

I want to add jdom.jar in my project, I do project-> properties-> Java Build Path->libraries-> add external jar jdom-2.0.5, but the import is not considered. how can I make available jdom.jar. this is the example that i want to test

 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());
  }
}
   }

Upvotes: 0

Views: 3627

Answers (1)

rolfl
rolfl

Reputation: 17707

JDOM 2.0.5 uses a different API (slightly) than your example code. Because some projects require both the original JDOM (without Generics) and the new JDOM with Generics, the decision was made to rename the JDOM package to org.jdom2.*

In almost every case it is simply a matter of changing your imports from import org.jdom.xxxxx to org.jdom2.xxxx

See this https://github.com/hunterhacker/jdom/wiki/JDOM2-Migration-Issues

Upvotes: 2

Related Questions