Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Easy Java/XML integration with JDOM, Part 1

Learn about a new open source API for working with XML

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 4 of 6

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


The first word after DOCTYPE indicates the name of the element being constrained, the word after PUBLIC is the document type's public identifier, and the last word is the document type's system identifier. The DocType is available by calling getDocType() on a Document, and the DocType class has methods to get the individual pieces of the DOCTYPE declaration.

 DocType docType = doc.getDocType();
 System.out.println("Element: " + docType.getElementName());
 System.out.println("Public ID: " + docType.getPublicID());
 System.out.println("System ID: " + docType.getSystemID());


Reading the element data

Every XML document must have a root element. That element is the starting point for accessing all the information within the document. For example, that snippet of a document has <web-app> as the root:

 <web-app id="demo">
  <description>Gotta fit servlets in somewhere!</description>
  <distributable/>
 </web-app>


The root Element instance is available on a Document directly:

 Element webapp = doc.getRootElement();


You can then access that Element's attributes (for example, the id above), content, and child Elements.

Playing with children

XML documents are tree structures, and any Element may contain any number of child Elements. For example, the <web-app> element has <description> and <distributable> tags as children. You can obtain an Element's children with various methods. getChild() returns null if no child by that name exists.

List getChildren(); // return all children
List getChildren(String name); // return all children by name
Element getChild(String name); // return first child by name


To demonstrate:

  // Get a List of all direct children as Element objects
  List allChildren = element.getChildren();
  out.println("First kid: " + ((Element)allChildren.get(0)).getName());
  // Get a list of all direct children with a given name
  List namedChildren = element.getChildren("name");
  // Get a list of the first kid with a given name
  Element kid = element.getChild("name");


Using getChild() makes it easy to quickly access nested elements when the structure of the XML document is known in advance. Given that XML:

<?xml version="1.0"?>
<linux:config>
  <gui>
    <window-manager>
      <name>Enlightenment</name>
      <version>0.16.2</version>
    </window-manager>
    <!-- etc -->
  </gui>
</linux:config>


That code directly retrieves the current window manager name:

String windowManager = rootElement.getChild("gui")
                                  .getChild("window-manager")
                                  .getChild("name")
                                  .getText();


Just be careful about NullPointerExceptions if the document has not been validated. For simpler document navigation, future JDOM versions are likely to support XPath references. Children can get their parent using getParent().

Getting element attributes

Attributes are another piece of information that elements hold. They're familiar to any HTML programmer. The following <table> element has width and border attributes.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (12)
Login
Forgot your account info?

new style ed hardy jackets discount gucci shoesBy Anonymous on September 1, 2010, 7:43 am Cheap louis vuitton shoes Replica handbags,purse brand Lip Stick china gucci shoes wholesale Wholesale puma shoes 3 color eye shadow replica Air Max 2010/24-7 Fashion...

Reply | Read entire comment

I just cant stop readingBy Anonymous on August 29, 2010, 3:45 pmI just cant stop reading this. Its so cool, so full of information that I just didn’t know. Im glad to see that people are actually writing about this issue in such...

Reply | Read entire comment

accept paypal cole haan men shoes,air maBy Anonymous on August 24, 2010, 9:55 am [url = http://www.airjordan4sale.com ]wholesale gucci clothing[/url] [url = http://www.airjordan4sale.com ]wholesale gucci shoes[/url] [url = http://www.airjordan4sale.com...

Reply | Read entire comment

gucci bagsBy Anonymous on August 16, 2010, 3:40 amgucci bags gucci bag gucci handbags gucci handbag discount gucci handbags

Reply | Read entire comment

our coach outlet storeBy Anonymous on July 6, 2010, 2:24 amour coach outlet store locations offer coach sale,coach bag outlet,Coach bags,Coach Wallet,Coach Sunglasses welcome go to visit http://www.topcoachoutlet.com. ...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources