The Original Java Almanac Website

The original version of this site was called The Java Developers Almanac which contained all the examples from the book. The site was created in 2002 and was purely static. Each time I created a new example, I had to regenerate and upload the entire site. The source content that was used to create the book was used in the generation of the site. One of the requirements I had for the source content was that it be 100% compilable and that any markup needed for the final presentation, would be embedded in the source code. I had to be creative with the use of inline comments in order to satisfy this requirement. Figure 1 below shows an example of the source content.

From the source content, I used perl to generate an XML version of the source. Figure 2 below shows an example of the source content in its XML form.

To create the book, I used XSLT (see Figure 3 below) to convert the example in XML form into data with TeX markup. Figure 4 below shows an example of the source content in TeX format. Art Ogawa, a veritable TeX expert, created the macros to convert the TeX input into PDF files.

To create the website, I modified the XSLT to convert the XML form into HTML.

Figure 1: Example of Source Content

/*
Finding Elements by Absolute Location in a DOM Document Using XPath

XPath is an expression language for selecting nodes in an XML file.
For example, the XPath expression \cv{/a/b} selects all \cv{b}
elements under the root element \cv{a}.  This example demonstrates
some common XPath expressions for selecting elements based on the
document root.  For examples of XPath expressions relative to a
particular node, see \see{org.w3c.dom/xpath/GetRelElem.java}.  For
more information about XPath, see the specification at
\link{http://www.w3c.org/TR/xpath}.

The XPath package used in this example selects nodes in a DOM
document. Hence, the XML file is first parsed into a DOM document and
the XPath expression is then applied to the DOM document.  The result
of an XPath expression is shown next to the expression; the numbers
are ids of elements in the sample file shown at the end of this
example.

\par{} Note: This example uses the XPath packages (\cv{org.apache.xpath.*})
available in J2SE 1.4.  However, these packages are not yet part of
the Java 2 API and so this example may not work in future versions of
J2SE.

*/

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class GetAbsElem {
    public static void main(String[] args) {
        try {
            // Create a document; this method is implemented in 
            // \see{javax.xml.parsers/BasicDom.java}
            Document doc = /*p*/Utils./**//*i*/parseXmlFile/**/(/*i*/args[0]/*"infilename.xml"*/, /*i*/false/**/);

//
            // Get the root element (without specifying its name)
            String xpath = "/*";                       // 1
//
            print(doc, xpath);
//

            // Get the root element (using its name)
            xpath = "/root";                           // 1
//
            print(doc, xpath);
//

            // Get all elements directly under the root
            xpath = "/root/*";                         // 2 8 12
//
            print(doc, xpath);
//

            // Get all e elements directly under the root
            xpath = "/root/e";                         // 12
//
            print(doc, xpath);
//

            // Get all e elements in the document
            xpath = "//e";                             // 4 6 10 11 12
//
            print(doc, xpath);
//

            // Get all non-e elements in the document
            xpath = "//*[name() != 'e']";              // 1 2 3 5 7 8 9
//
            print(doc, xpath);
//

            // Get all e elements directly under an elem1 element
            xpath = "//elem1/e";                       // 10 11
//
            print(doc, xpath);
//

            // Get all e elements anywhere under an elem1 element
            xpath = "//elem1//e";                      // 4 6 10 11
//
            print(doc, xpath);
//

            // Get all elements with at least one child element
            xpath = "//*[*]";                          // 1 2 3 5 8
//
            print(doc, xpath);
//

            // Get all elements without a child element
            xpath = "//*[not(*)]";                     // 4 6 7 9 10 11 12
//
            print(doc, xpath);
//

            // Get all elements with at least one child e element
            xpath = "//*[e]";                          // 1 3 5 8
//
            print(doc, xpath);
//

            // Get all elements with more than one child e elements
            xpath = "//*[count(e)>1]";                 // 8
//
            print(doc, xpath);
//

            // Get all non-e elements without an e child element
            xpath = "//*[not(e) and name() != 'e']";   // 2 7 9
//
            print(doc, xpath);
//

            // Get all \meta{level-4} e elements (the root being at level 1)
            xpath = "/*/*/*/e";                        // 4
//
            print(doc, xpath);
//

            // Get all elements with more than one child e elements
            xpath = "//*[count(e)>1]";                 // 8
//
            print(doc, xpath);
/*

XPath 1.0 does not support regular expressions to match element names.
However, it is possible to perform some very simple matches on element
names.

*/
//
            // Get all elements whose name starts with el
            xpath = "//*[starts-with(name(), 'el')]";  // 2 3 5 7 8 9
//
            print(doc, xpath);
//

            // Get all elements whose name contains with lem1
            xpath = "//*[contains(name(), 'lem1')]";   // 2 8
//
            print(doc, xpath);
/*

Sets of elements can also be combined using the union operator \cv{|}

*/
//
            // Get all e elements directly under either the root or an elem2 element
            xpath = "/*/e | //elem2/e";                // 4 12
//
            print(doc, xpath);

        } catch (javax.xml.transform.TransformerException e) {
            e.printStackTrace();
        }
        String xpath = "/*";
/*

Elements are returned in \meta{document order}, that is, if the
location of \cv{A}'s start tag appears before the location of \cv{B}'s
start tag in the XML document, element \cv{A} is returned before
element \cv{B}.  Moreover, the returned elements are unique.

*/
//
        // Read an XML document; this method is implemented in 
        // \see{javax.xml.parsers/BasicDom.java}
        Document doc = /*p*/Utils./**//*i*/parseXmlFile/**/(/*i*/args[0]/*"infilename.xml"*/, /*i*/false/**/);

        try {
            // Get the matching elements
            NodeList nodelist = org.apache.xpath.XPathAPI.selectNodeList(doc, xpath);

            // Process the elements in the nodelist
            for (int i=0; i<nodelist.getLength(); i++) {
                // Get element
                Element elem = (Element)nodelist.item(i);
            }
        } catch (javax.xml.transform.TransformerException e) {
//
            e.printStackTrace();
//
        }
//
    }
    static void print(Document doc, String xpath) throws javax.xml.transform.TransformerException {
        System.out.println("---- "+xpath+" ----");
        print(org.apache.xpath.XPathAPI.selectNodeList(doc, xpath));
    }
    static void print(Node node) {
        System.out.print(node.getNodeName());
        System.out.print(" "+node.getNodeType());
        System.out.print(" "+node.getNodeValue());
        System.out.println(" "+((Element)node).getAttribute("id"));
    }
    static void print(NodeList nodelist) {
        for (int i=0; i<nodelist.getLength(); i++) {
            print(nodelist.item(i));
        }
        System.out.println("---");
    }
}
/*
Here is the sample XML file used in the example:
*/
//
//!GetAbsElem.xml
//

Figure 2: XML Form of the Source Content

<?xml version="1.0" encoding="ISO-8859-1" ?>
<example oname="xpath_GetAbsElem" num="1074" file="org.w3c.dom/xpath/GetAbsElem.java" package="org.w3c.dom" subtitle="XPath" rootname="index" egscount="30" subcount="5">
  <example-title>Finding Elements by Absolute Location in a DOM Document Using XPath</example-title>
<desc-text><xsl-text>
XPath is an expression language for selecting nodes in an XML file.
For example, the XPath expression </xsl-text><code><xsl-text>/a/b</xsl-text></code><xsl-text> selects all </xsl-text><code><xsl-text>b</xsl-text></code><xsl-text>
elements under the root element </xsl-text><code><xsl-text>a</xsl-text></code><xsl-text>.  This example demonstrates
some common XPath expressions for selecting elements based on the
document root.  For examples of XPath expressions relative to a
particular node, see </xsl-text><see num="X" ofile="../org.w3c.dom/xpath_GetRelElem" file="org.w3c.dom/xpath/GetRelElem.java">X</see><xsl-text>.  For
more information about XPath, see the specification at
</xsl-text><link>http://www.w3c.org/TR/xpath</link><xsl-text>.

The XPath package used in this example selects nodes in a DOM
document. Hence, the XML file is first parsed into a DOM document and
the XPath expression is then applied to the DOM document.  The result
of an XPath expression is shown next to the expression; the numbers
are ids of elements in the sample file shown at the end of this
example.

</xsl-text><paragraph/><xsl-text> Note: This example uses the XPath packages (</xsl-text><code><xsl-text>org.apache.xpath.*</xsl-text></code><xsl-text>)
available in J2SE 1.4.  However, these packages are not yet part of
the Java 2 API and so this example may not work in future versions of
J2SE.

</xsl-text></desc-text>
<code-text>
  <code-line><xsl-text>// Get the root element (without specifying its name)</xsl-text></code-line>
  <nopagebreak/>
  <code-line><xsl-text>String xpath = "/*";                       // 1</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get the root element (using its name)</xsl-text></code-line>
  <code-line><xsl-text>xpath = "/root";                           // 1</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all elements directly under the root</xsl-text></code-line>
  <code-line><xsl-text>xpath = "/root/*";                         // 2 8 12</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all e elements directly under the root</xsl-text></code-line>
  <code-line><xsl-text>xpath = "/root/e";                         // 12</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all e elements in the document</xsl-text></code-line>
  <code-line><xsl-text>xpath = "//e";                             // 4 6 10 11 12</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all non-e elements in the document</xsl-text></code-line>
  <code-line><xsl-text>xpath = "//*[name() != 'e']";              // 1 2 3 5 7 8 9</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all e elements directly under an elem1 element</xsl-text></code-line>
  <code-line><xsl-text>xpath = "//elem1/e";                       // 10 11</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all e elements anywhere under an elem1 element</xsl-text></code-line>
  <code-line><xsl-text>xpath = "//elem1//e";                      // 4 6 10 11</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all elements with at least one child element</xsl-text></code-line>
  <code-line><xsl-text>xpath = "//*[*]";                          // 1 2 3 5 8</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all elements without a child element</xsl-text></code-line>
  <code-line><xsl-text>xpath = "//*[not(*)]";                     // 4 6 7 9 10 11 12</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all elements with at least one child e element</xsl-text></code-line>
  <code-line><xsl-text>xpath = "//*[e]";                          // 1 3 5 8</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all elements with more than one child e elements</xsl-text></code-line>
  <code-line><xsl-text>xpath = "//*[count(e)&gt;1]";                 // 8</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all non-e elements without an e child element</xsl-text></code-line>
  <code-line><xsl-text>xpath = "//*[not(e) and name() != 'e']";   // 2 7 9</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all </xsl-text><italics><xsl-text>level-4</xsl-text></italics><xsl-text> e elements (the root being at level 1)</xsl-text></code-line>
  <code-line><xsl-text>xpath = "/*/*/*/e";                        // 4</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all elements with more than one child e elements</xsl-text></code-line>
  <nopagebreak/>
  <code-line><xsl-text>xpath = "//*[count(e)&gt;1]";                 // 8</xsl-text></code-line>
</code-text>
<desc-text><xsl-text>
XPath 1.0 does not support regular expressions to match element names.
However, it is possible to perform some very simple matches on element
names.

</xsl-text></desc-text>
<code-text>
  <code-line><xsl-text>// Get all elements whose name starts with el</xsl-text></code-line>
  <nopagebreak/>
  <code-line><xsl-text>xpath = "//*[starts-with(name(), 'el')]";  // 2 3 5 7 8 9</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>// Get all elements whose name contains with lem1</xsl-text></code-line>
  <nopagebreak/>
  <code-line><xsl-text>xpath = "//*[contains(name(), 'lem1')]";   // 2 8</xsl-text></code-line>
</code-text>
<desc-text><xsl-text>
Sets of elements can also be combined using the union operator </xsl-text><code><xsl-text>|</xsl-text></code><xsl-text>

</xsl-text></desc-text>
<code-text>
  <code-line><xsl-text>// Get all e elements directly under either the root or an elem2 element</xsl-text></code-line>
  <nopagebreak/>
  <code-line><xsl-text>xpath = "/*/e | //elem2/e";                // 4 12</xsl-text></code-line>
</code-text>
<desc-text><xsl-text>
Elements are returned in </xsl-text><italics><xsl-text>document order</xsl-text></italics><xsl-text>, that is, if the
location of </xsl-text><code><xsl-text>A</xsl-text></code><xsl-text>'s start tag appears before the location of </xsl-text><code><xsl-text>B</xsl-text></code><xsl-text>'s
start tag in the XML document, element </xsl-text><code><xsl-text>A</xsl-text></code><xsl-text> is returned before
element </xsl-text><code><xsl-text>B</xsl-text></code><xsl-text>.  Moreover, the returned elements are unique.

</xsl-text></desc-text>
<code-text>
  <code-line><xsl-text>// Read an XML document; this method is implemented in</xsl-text></code-line>
  <nopagebreak/>
  <code-line><xsl-text>// </xsl-text><see num="510" ofile="../javax.xml.parsers/BasicDom" file="javax.xml.parsers/BasicDom.java">The Quintessential Program to Create a DOM Document from an XML File</see><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>Document doc = </xsl-text><italics><xsl-text>parseXmlFile</xsl-text></italics><xsl-text>(</xsl-text><italics><xsl-text>"infilename.xml"</xsl-text></italics><xsl-text>, </xsl-text><italics><xsl-text>false</xsl-text></italics><xsl-text>);</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><xsl-text>try {</xsl-text></code-line>
  <code-line><indent/><xsl-text>// Get the matching elements</xsl-text></code-line>
  <code-line><indent/><xsl-text>NodeList nodelist = org.apache.xpath.XPathAPI.selectNodeList(doc, xpath);</xsl-text></code-line>
  <code-line><xsl-text></xsl-text></code-line>
  <code-line><indent/><xsl-text>// Process the elements in the nodelist</xsl-text></code-line>
  <code-line><indent/><xsl-text>for (int i=0; i&lt;nodelist.getLength(); i++) {</xsl-text></code-line>
  <code-line><indent/><indent/><xsl-text>// Get element</xsl-text></code-line>
  <code-line><indent/><indent/><xsl-text>Element elem = (Element)nodelist.item(i);</xsl-text></code-line>
  <code-line><indent/><xsl-text>}</xsl-text></code-line>
  <code-line><xsl-text>} catch (javax.xml.transform.TransformerException e) {</xsl-text></code-line>
  <nopagebreak/>
  <code-line><xsl-text>}</xsl-text></code-line>
</code-text>
<desc-text><xsl-text>Here is the sample XML file used in the example:
</xsl-text></desc-text>
<code-text>
  <code-line><xsl-text>&lt;?xml version="1.0" encoding="UTF-8"?&gt;</xsl-text></code-line>
  <nopagebreak/>
  <code-line><xsl-text>&lt;root id="1"&gt;</xsl-text></code-line>
  <code-line><indent/><xsl-text>&lt;elem1 id="2"&gt;</xsl-text></code-line>
  <code-line><indent/><indent/><xsl-text>&lt;elem2 id="3"&gt;</xsl-text></code-line>
  <code-line><indent/><indent/><indent/><xsl-text>&lt;e id="4"/&gt;</xsl-text></code-line>
  <code-line><indent/><indent/><indent/><xsl-text>&lt;elem3 id="5"&gt;</xsl-text></code-line>
  <code-line><indent/><indent/><indent/><indent/><xsl-text>&lt;e id="6"/&gt;</xsl-text></code-line>
  <code-line><indent/><indent/><indent/><xsl-text>&lt;/elem3&gt;</xsl-text></code-line>
  <code-line><indent/><indent/><indent/><xsl-text>&lt;elem3 id="7"/&gt;</xsl-text></code-line>
  <code-line><indent/><indent/><xsl-text>&lt;/elem2&gt;</xsl-text></code-line>
  <code-line><indent/><xsl-text>&lt;/elem1&gt;</xsl-text></code-line>
  <code-line><indent/><xsl-text>&lt;elem1 id="8"&gt;</xsl-text></code-line>
  <code-line><indent/><indent/><xsl-text>&lt;elem2 id="9"/&gt;</xsl-text></code-line>
  <code-line><indent/><indent/><xsl-text>&lt;e id="10"/&gt;</xsl-text></code-line>
  <code-line><indent/><indent/><xsl-text>&lt;e id="11"/&gt;</xsl-text></code-line>
  <code-line><indent/><xsl-text>&lt;/elem1&gt;</xsl-text></code-line>
  <code-line><indent/><xsl-text>&lt;e id="12"/&gt;</xsl-text></code-line>
  <nopagebreak/>
  <code-line><xsl-text>&lt;/root&gt;</xsl-text></code-line>
</code-text>
</example>

Figure 3: XLST Program to Convert an Example in XML Form to TeX

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" indent="no"/>


<xsl:template match="example">
    <xsl:text>\open\example{\title{</xsl:text>
    <xsl:apply-templates select="example-title" mode="x"/>
    <xsl:text>}\id{</xsl:text>
    <xsl:value-of select="@num"/>
    <xsl:text>}}</xsl:text>

    <xsl:apply-templates/>

    <xsl:text>\close\example{}</xsl:text>
</xsl:template>


<xsl:template match="example-title" mode="x">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="example-title" >
</xsl:template>


<xsl:template match="desc-text//italics">
    <xsl:text>\meta{</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="desc-text//code">
    <xsl:text>\cv{</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="link">
    <xsl:text>\cv{</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>}</xsl:text>
</xsl:template>


<xsl:template match="code-text//italics">
    <xsl:text>\meta </xsl:text>
    <xsl:apply-templates/>
    <xsl:text>\?</xsl:text>
</xsl:template>


<xsl:template match="see">
    <xsl:text>\refex{</xsl:text><xsl:value-of select="text()"/><xsl:text>}{</xsl:text><xsl:value-of select="@num"/><xsl:text>}</xsl:text>
</xsl:template>


<xsl:template match="paragraph">
    <xsl:text>\par{}</xsl:text>
</xsl:template>


<xsl:template match="backslash">
    <xsl:text>\blash </xsl:text>
</xsl:template>


<xsl:template match="caret">
    <xsl:text>\caret </xsl:text>
</xsl:template>

<xsl:template match="percent">
    <xsl:text>\%</xsl:text>
</xsl:template>


<xsl:template match="xsl-text">
    <xsl:value-of select="text()"/>
</xsl:template>

 
<xsl:template match="desc-text">
    <xsl:apply-templates/>
</xsl:template>


<xsl:template match="code-text">
    <xsl:text>\open\verbatim{}</xsl:text>
    <xsl:apply-templates select="code-line|nopagebreak" />
    <xsl:text>
\close\verbatim{}</xsl:text>
</xsl:template>


<xsl:template match="code-line">
    <xsl:text>
</xsl:text>
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="indent">
    <xsl:text>\tab </xsl:text>
</xsl:template>

<xsl:template match="nopagebreak">
    <xsl:text>\nopagebreak</xsl:text>
</xsl:template>
 
<xsl:template match="related">
</xsl:template>

<xsl:template match="endash">
    <xsl:text>{--}</xsl:text>
</xsl:template>
<xsl:template match="emdash">
    <xsl:text>{---}</xsl:text>
</xsl:template>

</xsl:stylesheet>

Figure 4: TeX Version of the Source Content

\open\example{\title{Finding Elements by Absolute Location in a DOM Document Using XPath}\id{1074}}
  

XPath is an expression language for selecting nodes in an XML file.
For example, the XPath expression \cv{/a/b} selects all \cv{b}
elements under the root element \cv{a}.  This example demonstrates
some common XPath expressions for selecting elements based on the
document root.  For examples of XPath expressions relative to a
particular node, see \refex{X}{X}.  For
more information about XPath, see the specification at
\cv{http://www.w3c.org/TR/xpath}.

The XPath package used in this example selects nodes in a DOM
document. Hence, the XML file is first parsed into a DOM document and
the XPath expression is then applied to the DOM document.  The result
of an XPath expression is shown next to the expression; the numbers
are ids of elements in the sample file shown at the end of this
example.

\par{} Note: This example uses the XPath packages (\cv{org.apache.xpath.*})
available in J2SE 1.4.  However, these packages are not yet part of
the Java 2 API and so this example may not work in future versions of
J2SE.


\open\verbatim{}
// Get the root element (without specifying its name)\nopagebreak
String xpath = "/*";                       // 1

// Get the root element (using its name)
xpath = "/root";                           // 1

// Get all elements directly under the root
xpath = "/root/*";                         // 2 8 12

// Get all e elements directly under the root
xpath = "/root/e";                         // 12

// Get all e elements in the document
xpath = "//e";                             // 4 6 10 11 12

// Get all non-e elements in the document
xpath = "//*[name() != 'e']";              // 1 2 3 5 7 8 9

// Get all e elements directly under an elem1 element
xpath = "//elem1/e";                       // 10 11

// Get all e elements anywhere under an elem1 element
xpath = "//elem1//e";                      // 4 6 10 11

// Get all elements with at least one child element
xpath = "//*[*]";                          // 1 2 3 5 8

// Get all elements without a child element
xpath = "//*[not(*)]";                     // 4 6 7 9 10 11 12

// Get all elements with at least one child e element
xpath = "//*[e]";                          // 1 3 5 8

// Get all elements with more than one child e elements
xpath = "//*[count(e)>1]";                 // 8

// Get all non-e elements without an e child element
xpath = "//*[not(e) and name() != 'e']";   // 2 7 9

// Get all \meta level-4\? e elements (the root being at level 1)
xpath = "/*/*/*/e";                        // 4

// Get all elements with more than one child e elements\nopagebreak
xpath = "//*[count(e)>1]";                 // 8
\close\verbatim{}

XPath 1.0 does not support regular expressions to match element names.
However, it is possible to perform some very simple matches on element
names.


\open\verbatim{}
// Get all elements whose name starts with el\nopagebreak
xpath = "//*[starts-with(name(), 'el')]";  // 2 3 5 7 8 9

// Get all elements whose name contains with lem1\nopagebreak
xpath = "//*[contains(name(), 'lem1')]";   // 2 8
\close\verbatim{}

Sets of elements can also be combined using the union operator \cv{|}


\open\verbatim{}
// Get all e elements directly under either the root or an elem2 element\nopagebreak
xpath = "/*/e | //elem2/e";                // 4 12
\close\verbatim{}

Elements are returned in \meta{document order}, that is, if the
location of \cv{A}'s start tag appears before the location of \cv{B}'s
start tag in the XML document, element \cv{A} is returned before
element \cv{B}.  Moreover, the returned elements are unique.


\open\verbatim{}
// Read an XML document; this method is implemented in\nopagebreak
// \refex{The Quintessential Program to Create a DOM Document from an XML File}{510}
Document doc = \meta parseXmlFile\?(\meta "infilename.xml"\?, \meta false\?);

try {
\tab // Get the matching elements
\tab NodeList nodelist = org.apache.xpath.XPathAPI.selectNodeList(doc, xpath);

\tab // Process the elements in the nodelist
\tab for (int i=0; i<nodelist.getLength(); i++) {
\tab \tab // Get element
\tab \tab Element elem = (Element)nodelist.item(i);
\tab }
} catch (javax.xml.transform.TransformerException e) {\nopagebreak
}
\close\verbatim{}
Here is the sample XML file used in the example:

\open\verbatim{}
<?xml version="1.0" encoding="UTF-8"?>\nopagebreak
<root id="1">
\tab <elem1 id="2">
\tab \tab <elem2 id="3">
\tab \tab \tab <e id="4"/>
\tab \tab \tab <elem3 id="5">
\tab \tab \tab \tab <e id="6"/>
\tab \tab \tab </elem3>
\tab \tab \tab <elem3 id="7"/>
\tab \tab </elem2>
\tab </elem1>
\tab <elem1 id="8">
\tab \tab <elem2 id="9"/>
\tab \tab <e id="10"/>
\tab \tab <e id="11"/>
\tab </elem1>
\tab <e id="12"/>\nopagebreak
</root>
\close\verbatim{}

\close\example{}