You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xml.apache.org by John Robert Gardner <Jo...@east.sun.com> on 2000/09/11 16:52:44 UTC
Building DTD-compliant DOM
Again a question re. Xerces, and as I don't work with it enough to be on that
list, please forward it as need be if that's a better forum. I'm sure someone
has looked at this before. My colleague and I have looked for something like
this in archives and Faq's, but to no avail.
<snip>
Okay, the crux of the question is:
In Java, using Xerces, how do you build, from scratch, a DOM tree that is
compliant with a specified DTD?
What I am doing is this: I'm creating my DOM tree, appending the elements,
and writing it out to a file. I thought the DOM tree is supposed to be
compliant with the DTD I specified in my DocumentType object, because I
constructed it with a reference to my DocumentTypeImpl object:
outDoc = new DocumentImpl(dti); // dti is my DocumentTypeImpl
Further, when I spit out the document, it does NOT contain a reference to my
DTD -- shouldn't it be doing that?
Do I have to manually order the elements to be compliant with my DTD? If so,
how can I get access to the DTD itself to find out the order of the
elements? In the older (1.1) IBM4J parser, there was a DTD class that you
could use to find out the specification of the element order. In the Xerces
parser, I see no access to the DTD whatsoever.
I have attempted to use SAX, but abandoned the effort because I need
non-linear access to an original document (this effort is following
references that could be referenced in any order, I need to have
random-access to the document).
So -- any thoughts?
</snip>
another tyvmia,
jr
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-|
John Robert Gardner
Enterprise Management Architecture Group
Sun Microsystems Inc.,
MailStop UBUR02-306
1 Network Drive
Burlington, MA 01803-0903 | "Earn this"
|
Ph. 781-442-0692 | -Capt. John H. Miller
Fax 781-442-1539 | Saving Private Ryan
e-mail john.robert.gardner@sun.com
-----------------------------------
http://vedavid.org/diss/
http://vedavid.org/xml/docs/
Re: Building DTD-compliant DOM
Posted by Sean Kelly <ke...@mail2a.jpl.nasa.gov>.
John:
Try this:
Document toss = new DocumentImpl();
DocumentType docType =
toss.getImplementation().createDocumentType("rootElem",
"-//XYZ//DTD My Special DTD//EN", "http:...");
Document doc = toss.getImplementation().createDocument(null, "rootElem",
docType);
If you serialize doc at this point, you'll get:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rootElem PUBLIC "-//XYZ//DTD My Special DTD//EN"
"http://my.host/whatever.dtd">
<rootElem/>
That "null" argument to createDocument is the namespace URI. Set it if
you're using namespaces.
In my own projects, I hide calls to the *Impl classes in a general XML
service wrapper, so that the XML implementation can change withouth
affecting clients, so that first line goes away and becomes
Document toss = XML.createDocument();
(And in fact, there's an XML.createDTDDoc(...) method, too.)
Now, as for creating the doc nodes so they're compliant with the DTD ...
write careful code! :-) Well, seriously, I've never done that part before.
I also recall querying the DTD with XML4J, but I don't know how to do that
with Xerces either.
Good luck.
--Sean