Building a JDOM document
// Create the root element
Element carElement = new Element("car");
//create the document
Document myDocument = new Document(carElement);
//add an attribute to the root element
carElement.addAttribute(new Attribute("vin", "123fhg5869705iop90"));
//add a comment
carElement.addContent(new Comment("Description of a car"));
//add some child elements
Element make = new Element("make");
make.addContent("Toyota");
carElement.addContent(make);
//add some more elements
carElement.addContent(new Element("model").addContent("Celica"));
carElement.addContent(new Element("year").addContent("1997"));
carElement.addContent(new Element("color").addContent("green"));
carElement.addContent(new Element("license")
.addContent("1ABC234").addAttribute("state", "CA"));
|
Return to the article.