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

Marc Palmer marc at anyware.co.uk
Mon Jan 10 04:08:44 PST 2005


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 at wangjammers.org
			
                         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



More information about the jdom-interest mailing list