Possible to validate XML against a DTD using Java 5/JAXB/etc?

T

techguy_chicago

There seems to be a lot of deprecated API stuff going on in Java, so
this has added to my confusion. Ideally, I want some code to do this,
without adding any additional JARs to my project (using Java 5 and
probably have xerces (same, different?), too):

if ( isValidXml(path_to_xml_file, path_to_dtd_file) ) {
// then celebrate like U.S. just won the world cup
}
else {
// or, much more likely scenario, entire team gets deserved red
cards
// for lying down on the job. they should all be fined.
}

That's it. That's all I want. A method called 'isValidXml()' that
validates an xml file against a DTD. I've done this many times in the
past - I swear, but Java and XML are now sophisticated, apparently.
I've been complexed-out of the market.

I'm willing to use JDOM and all that if I have to, but do I really have
to?

We can address a specific error if you like. I would like to do
something like this page suggests
(http://java.sun.com/developer/technicalArticles/xml/jaxp1-3/#Schema_Validation_Framework),
using setSchema() and other new, fun APIs, but when plugging in a
'DTD' 'schema' for the 'xsd', I get an error.

Here is my code:
-----------------
String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
SchemaFactory factory = SchemaFactory.newInstance(language);
factory.setErrorHandler(new MyErrorHandler());
//factory.setResourceResolver( new MyLSResourceResolver());
//StreamSource ss = new StreamSource(new File("mySchema.xsd")));
StreamSource ss = new StreamSource(new File("myDtd.dtd")));
Schema schema = factory.newSchema(ss);
----------------

The error is:

java.lang.IllegalArgumentException: http://www.w3.org/TR/REC-xml


Of course, I've tried different XMLConstants, different error handlers,
etc. I've even tried old-school sax and dom stuff, all to no avail. I
can't get valid documents to validate and invalid documents to
invalidate.

I'd think it was all bizarre, but after doing java for 8 or so years
now, I've learned that this is just Java. Job security.

If nothing soon, I'll convert this DTD to a real Schema, but I'd rather
no go there until I can force my client to do the same. If there's no
other option, however, I'm there.

I tried the multi-validation sun classes, too. Nada.

Thanks for any pointers (to the nearest bridge)!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top