Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Simplify XML programming with JDOM

This open-source API makes XML document manipulation easy for Java developers


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.