Java Reference
In-Depth Information
<position x="10" y="10"/>
<bounds x="10" y="10"
width="42" height="42"/>
</circle>
<line angle="0.0">
<color R="0" G="0" B="255"/>
<position x="10" y="10"/>
<bounds x="10" y="10"
width="22" height="32"/>
<endpoint x="30" y="40"/>
</line>
<rectangle width="30.0" height="20.0" angle="0.0">
<color R="255" G="0" B="0"/>
<position x="30" y="40"/>
<bounds
x="30" y="40"
width="32" height="22"/>
</rectangle>
</sketch>
Directory "TryParsingSchemaInstance"
This defines a sketch that consists of three elements: a circle, a line, and a rectangle. The location of the
schema is specified by the value for the
noNamespaceSchemaLocation
attribute, which here corresponds to
the
Sketcher.xsd
file in the
Beg Java Stuff
directory. You can save the document as
sketch1.xml
in the
same directory.
An XML document may have the applicable schema identified by the value for the
noNamespaceSchem-
aLocation
attribute, or the schema may not be identified explicitly in the document. You have the means
for dealing with both of these situations in Java.
A
javax.xml.validation.Schema
object encapsulates a schema. You create a
Schema
object by calling
methods for a
javax.xml.validation.SchemaFactory
object that you generate by calling the static
newInstance()
method for the
SchemaFactory
class. It works like this:
SchemaFactory sf =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
A
SchemaFactory
object compiles a schema from an external source in a given Schema Definition Lan-
guage into a
Schema
object that can subsequently be used by a parser. The argument to the static
newIn-
stance()
method in the
SchemaFactory
class identifies the schema language that the schema factory can
process. The only alternative to the argument used in the example to specify the XML Schema language
is
“
http://relaxng.org/ns/structure/1.0
,” which is another schema language for XML. At the time of writing,
Schema
objects encapsulating DTDs are not supported.
The
javax.xml.XMLConstants
class defines
String
constants for basic values required when you are
processing XML. The class defines a constant with the name
W3C_XML_NS_URI
that corresponds to the URI
for the Schema Definition Language, so you could use this as the argument to the
newInstance()
method
in the
SchemaFactory
class, like this:
SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_NS_URI);
This statement assumes you have statically imported the names from the
XMLConstants
class.