Creating a DOM document with validation?

S

Stefan Ram

I have found a nice example of how to create an XML document
using DOM with Java:

http://www.cafeconleche.org/books/xmljava/chapters/ch10s03.html

Now, my problem is: When I modify the element type name "desc"
to "slrdesc", the document is still built with the new element
type name "slrdesc". I assume, that the SVG-DTD does not allow
such an element type name "slrdesc" and that therefore the
document created is invalid.

I'd like to have a validating document builder, i.e., in the
very moment, when I am attempting to insert a node that would
make the tree invalid, I want the builder to refuse this
attempt and to report an error, possibly throw an exception or
so. The document of the example above knows its DTD, but it
does not seem to do this validation. Is there a library for
Java to build a DOM (or something like this) with validation
at the momemt an attempt to modify the document is made?
(I would like to use this to create XHTML 1.x documents.)

Or is there at least an explicit call to validate the DOM,
which I have missed?
 
M

Martin Honnen

Stefan said:
I have found a nice example of how to create an XML document
using DOM with Java:

http://www.cafeconleche.org/books/xmljava/chapters/ch10s03.html

Now, my problem is: When I modify the element type name "desc"
to "slrdesc", the document is still built with the new element
type name "slrdesc". I assume, that the SVG-DTD does not allow
such an element type name "slrdesc" and that therefore the
document created is invalid.

I'd like to have a validating document builder, i.e., in the
very moment, when I am attempting to insert a node that would
make the tree invalid, I want the builder to refuse this
attempt and to report an error, possibly throw an exception or
so. The document of the example above knows its DTD, but it
does not seem to do this validation. Is there a library for
Java to build a DOM (or something like this) with validation
at the momemt an attempt to modify the document is made?

DOM Level 3 has a validation module:
http://www.w3.org/TR/2004/REC-DOM-Level-3-Val-20040127/
which allows you to manipulate the document with respect to a DTD and/or
schema I think.
I am not sure there are any implementations yet, the implementation report
http://www.w3.org/2003/07/DOM-Level-3-Val-implementations.html
list Oracle and other as providing implementations.
 
S

Stefan Ram

Martin Honnen said:

Here is a method to validate an in-memory DOM. It serializes
the document and reads it back again, doing the validation.

http://cvs.sourceforge.net/viewcvs....ic-Java/DOMValidator.java?rev=1.1&view=markup

OF course, I was looking for something more fine tuned, so
that when an attribute is added to an element, it would just
check whether that attribute might be added to this element
and so.

I thought that something like this was needed whenever a
document is being built and did not expect that it is so
difficult to find.

By the above method, I am supposed to serialize the whole DOM
and read it in again, validating everything again, which seems
to waste processor time.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top