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

Discussion in 'Java' started by techguy_chicago@yahoo.com, Jun 20, 2006.

  1. Guest

    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)!
     
    , Jun 20, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Jessel
    Replies:
    0
    Views:
    422
    Ben Jessel
    Aug 5, 2004
  2. Robert Dodier
    Replies:
    3
    Views:
    443
    james anderson
    Nov 4, 2003
  3. Adib Taraben
    Replies:
    1
    Views:
    604
    Patrick TJ McPhee
    May 24, 2004
  4. Andrew Thompson

    Validate XML against DTD? XSD OK. SSCCE.

    Andrew Thompson, Oct 3, 2007, in forum: Java
    Replies:
    5
    Views:
    2,864
    Roedy Green
    Oct 4, 2007
  5. Arved Sandstrom
    Replies:
    41
    Views:
    1,405
    Robert Klemme
    Mar 13, 2013
Loading...

Share This Page