Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

[jdom-interest] A hack for DTD validating a modified DOM

Marc Palmer

2005-01-10

Replies:


Hi,

I'm reasonably new to JDOM but had some good progress very quickly.

I haven't noticed this in the archive, and a few people have asked about
how to validate a DOM that has been constructed/modified
programmatically against a DTD (or Schema).

I was going to ask this on the list myself until I hit upon the ugly,
but reliable and immediately available solution - write the DOM out to a
stream and load it back in again with validation.

  /**
   * Re-validate the DOM
   */
  public void validate(Document doc) throws JDOMException
  {
     XMLOutputter out = new XMLOutputter();
     ByteArrayOutputStream tempOutstream = new
        ByteArrayOutputStream(50000); // Adjust for approx doc size
     try
     {
        out.output( doc, tempOutstream);
        byte[] buff = tempOutstream.toByteArray();
        SAXBuilder builder = newSAXBuilder(true);
        // Do the validation and discard resulting doc
        Document d = builder.build(new ByteArrayInputStream(buff),
          baseURL);
     }
     catch ( IOException e )
     {
        throw new JDOMException( "Failed to validate, error writing
           to memory stream", e);
     }
  }

It hurts to write such a hack - it there was any way to get a parser (or
JDOM) to validate against a DTD/Schema by invoking some "validate"
method, it would be great - but it's a problem of validation being part
of (today's) parsers surely?

Does anyone know if there are moves to separate parsing +
well-formedness checks from validation, in terms of APIs?

Cheers


--
Marc Palmer
wj5@(protected)
     
                w a n g j a m m e r s

           java and web software design
         experts with an ethical outlook

            http://www.wangjammers.org

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.