Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
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());
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.
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().
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.