Of course, this only scratches the surface of the kinds of
things you can do with XML.
Sample XML Files
A handful of sample XML files have been provided in the "samples"
directory:
// Create an instance of the DocumentBuilderFactory DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance(); //Get the DocumentBuilder from the factory that we just got above. DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); // turn it into an in-memory object doc = docBuilder.parse(new File (argv [0]));
Try switching the default parser when you use this example,
by setting the javax.xml.parsers.validation
system property
to true
.
XML Namespace Support
This library includes basic support for accessing XML namespace
information. XML namespaces support a simple lexical scoping
mechanism to help assign a "namespace" to elements and attributes.
Such namespaces are URIs, such as http://www.example.com/.
A simple file, main.java, demonstrates
how to access namespace information.
Note that accessing XML namespaces is not part of DOM Level 1;
this is an (essential) extension.
for Swing JTree Display
This example uses two kinds of element customizations:
swing.tree.TreeNode
and so can be used directly as
a swing.JTree
model.
There is a common driver, which can be given different customization and program data. One command shows "Richard III" as a tree of XML data. Another shows "Two Gentlemen of Verona" using custom element classes to control the structure that's displayed. (See the Makefile; you may need to modify this to point to the version of SWING which you're installed, if you're not using a JDK 1.2 release.)
$ make doit1 $ make doit2
Alternatively, these are accessible as applets from viewers (such as the JDK 1.2 "appletviewer") which support JFC 1.1:
This simple example shows how an XML document can be read and converted to use a different document encoding. (This is called transcoding. This can't be done with all text formats, since they weren't all specified to support labeling or autodetection of the document character set; XML does support this.
Read the source to this program
to see how the encoding detection support of this parser can be
used to support transcoding.