[jdom-interest] Question on an FAQ

Bill Woodward wpwood at saifa.net
Tue Apr 1 15:49:28 PST 2003


Hello All,

I have an XML document that represents a mapping of named properties to
values.  The format of the document is something like this:

<job>
   <job-name>test-job</job-name>
   <job-owner>wpwood</job-owner>
   <creation-time>4/1/03 17:35</creation-time>
   ... etc. ...
</job>

This document is validated against an XSD.  I'm using the Xerces parser, as
described in the FAQ, and everything works great.  When I first call
SAXBuilder.build() to create a JDOM Document, a JDOMException is thrown if
the document does not conform to the schema.

The problem is that I need to allow a developer to add a property to the
document via a utility class, which obviously just adds another element
child to the root element for the Document.  When I do this, if the property
name does not conform to the schema, it is still inserted.

It looks like this is the situation described by the following FAQ:

--- snip ---
Q: JDOM ensures the document in memory is always well-formed. Can JDOM also
ensure the document in memory is always valid?

A: No, it's our current belief that it's better to expose a checkValid()
type of call than to attempt constant validation checking. One reason is
performance. A second reason is that you have a chicken-and-egg problem
where for example an element needs exactly two child elements to be valid,
but after adding either child the document will be in a temporarily invalid
state. To work around this would require something like transactional
modifications, and that's a lot of overhead for little gain.
--- snip ---

Assuming that my understanding is correct, am I missing an obvious,
efficient implementation of 'checkValid()'?  The quick and dirty version is
to simply export the Document via an XMLOutputter and rebuild it via the
validating SAXParser.  Bleah...  Surely there's a better way.

Anyone have any hints?  Am I missing something?  I'm working with Beta 8,
but could probably go to a nightly build if I needed to.

TIA,
- Bill

      /------------------------------------\
     /   Bill Woodward (wpwood at saifa.net)   \
     \        http://www.saifa.net          /
      \------------------------------------/





More information about the jdom-interest mailing list