Java Reference
In-Depth Information
dbFactory
.
setNamespaceAware
(
true
true
);
dbFactory
.
setAttribute
(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage"
,
XMLConstants
.
W3C_XML_SCHEMA_NS_URI
);
}
}
DocumentBuilder parser
=
dbFactory
.
newDocumentBuilder
();
// If not using schema, Get local copies of DTDs...
iif
(
schema
==
null
null
) {
parser
.
setEntityResolver
(
new
new
MyDTDResolver
());
}
parser
.
parse
(
xmlFile
);
System
.
out
.
println
(
"Parsed/Validated OK"
);
}
}
// Just +catch+ statements below here...
To enable validation for Schema, DTD, RelaxNG, etc., you need to set validation true on the
DocumentBuilderFactory
before creating the
DocumentBuilderParser
. The
XParse
pro-
gram has code for doing this; it's done differently for DTD (
-v
) and Schema (
-v
and
-a
schemalocation
).
DOM also provides tools to traverse the document. You can use the defined
TreeWalker
in-
terface, or you can just use the algorithm shown in
Example 20-9
.
Example 20-9. XTW.java
public
public class
class
XTW
XTW
{
public
public static
void
main
(
String
[]
av
) {
iif
(
av
.
length
==
0
) {
System
.
err
.
println
(
"Usage: XTW file [...]"
);
return
static
void
return
;
}
for
for
(
int
int
i
=
0
;
i
<
av
.
length
;
i
++) {
String name
=
av
[
i
];
new
new
XTW
().
convert
(
name
,
true
true
);
}
}
/** Convert the file */
protected
protected
void
void
convert
(
String fileName
,
boolean
boolean
verbose
) {
try
try
{
iif
(
verbose
)