Java Reference
In-Depth Information
<!ATTLIST position x CDATA #REQUIRED>
<!ATTLIST position y CDATA #REQUIRED>
Document body contents are:
Node: circle
Node Type: Element
Element Attributes are:
diameter = 30
Child Nodes of circle are:
Node: position
Node Type: Element
Element Attributes are:
x = 30
y = 50
How It Works
All the new code to handle attributes is in the
listNodes()
method. After verifying that the current
node does have attributes, you get the collection of attributes as a
NamedNodeMap
object. You then iterate
through the collection extracting each node in turn. Nodes are indexed from zero, and you obtain the
number of nodes in the collection by calling its
getLength()
method. Because an attribute node is
returned by the
item()
method as type
Node
, you have to cast the return value to type
Attr
to call
the methods in this interface. You output the attribute and its value, making use of the
getName()
and
getValue()
methods for the
Attr
object in the process of assembling the output string.
It isn't used in the example, but the
Attr
interface also declares a
getSpecified()
method that returns
true
if the attribute value was explicitly set in the document rather than being a default value from the
DTD. The
Attr
interface also declares a
getOwnerElement()
method that returns an
Element
reference
to the element to which this attribute applies.
TRANSFORMING XML
The Extensible Stylesheet Language (XSL) is a standard language for describing how an XML document
should be transformed and/or displayed. XSL has three parts to it, referred to as the XSL family:
• XSL-FO is a standard language for formatting XML documents.
• XSLT is a standard language for transforming an XML document into another XML document, so
you could transform an XML document into HTML or XHTML, for example.
• XPath is a language for describing how you navigate through an XML document.
XSL is a huge topic that is generally far beyond the scope of this topic. Indeed, you can find whole topics
dedicated to the topic, so it is impossible for me to discuss it at length here. Nonetheless, the JAXP XSLT
capability provided by
javax.xml.transform.Transformer
objects can be very helpful when you want
to transfer an XML document to or from an external file, so I'm just explaining how you can use the Java
support for XSLT to do that.
Transformer Objects