Repository Interface

Wednesday, November 01, 2006

The following program shows creating a xml document using JDOM API

import org.jdom.*;

public class example1
{
public static void main(String[] args)
{
Element root = new Element("myRootElement");
Document doc = new Document(root);
root.setText("This is a root element");
System.out.println(doc);
}
}


The file begins with the import of the JDOM package. Inside the main method, the code declares a variable of type Element (a JDOM class), and instantiates it by calling the Element constructor.

The Element class has multiple constructors. The simplest one takes a single String that becomes the name of the element.

constructor, passing in the new After creating an Element, a Document object is instantiate by calling the DocumentElement.Once the Document is created, the code sets some text content for the root element. Finally, the code prints out the Document object.

If you compile and run this code you should see these results:

[Document: No DOCTYPE declaration, Root is [Element: ]]

Doesn't it look like XML?

The reason is that when a document object is used in a String context ,the toString() method is called by default.For a document,this emits a somewhat verbose delineation of the object,which can be handy during debugging.

0 Comments:

Post a Comment

<< Home