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



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)!

.



Relevant Pages

  • Re: xsd files only: enough to start writing app. code of a DB-related component ?
    ... junior Java programmer (know Java but this is a J2EE project ... These schema definition files aren't very numerous (about 20 ... claims to be able to generate database structure. ...
    (comp.lang.java.programmer)
  • Re: Schema Change Error
    ... > We have a Java application which is running for 24 hours and we have ... We are connected to SQL Server ... > We get the above error when there is a schema change i.e. ... something concurrently that your cursor relies on. ...
    (microsoft.public.sqlserver.jdbcdriver)
  • Jaxb and xsd Help requested
    ... (Note the xsd below is part of a bigger xsd file i ... the source schema. ... Java content class for GuestCountType complex type. ...
    (comp.lang.java.programmer)
  • Schema Change Error
    ... JDBC]Could not complete cursor operation because the table schema ... We have a Java application which is running for 24 hours and we have ... We get the above error when there is a schema change i.e. ... intermittent error i.e. some bug in SQL Server. ...
    (microsoft.public.sqlserver.jdbcdriver)
  • Re: XML configurable formatter/"Pretty Printer"
    ... is there anything Java based that can do this? ... >already existing formatter? ... You made a comment that suggested you might need a DTD or equivalent ... schemas could be hard to come by. ...
    (comp.lang.java.programmer)