[jdom-interest] Problem with DOMOutputter

Jason Hunter jhunter at collab.net
Thu Mar 1 20:33:41 PST 2001


> I'm trying to use the DOMOutputter and keep getting the following error:
> 
> org.jdom.JDOMException: Exception outputting Document: DOM006 Hierarchy
> request error

Alex sent me the code and here's my diagnosis.

Everything works OK with Xerces 1.2 for me, and that's the version we
ship by default with JDOM.  I could reproduce the problem with Xerces
1.3 using the following simple program:

import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

public class Tester12 {

  public static void main(String[] args) throws Exception {
    Document d = new SAXBuilder().build(args[0]);
    org.w3c.dom.Document domDoc = new DOMOutputter().output(d);
  }
}

The error is due to the fact that with Xerces 1.3 when you create a new
org.w3c.dom.Document it pre-creates a DOCTYPE and root element.  Here's
a debug stmt from DOMOutputter showing this:

domDoc before add is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root><root />

So then when you appendChild() the new root, DOM detects you're
appending two root elements and you get the DOM error message "DOM006
Hierarchy request error".  Gotta love those DOM error messages.  So easy
to understand.

I made a fix in DOMOutputter so if there's a root we call replaceChild()
instead of appendChild().  It's only a partial fix because it doesn't
change the DOM doc's DOCTYPE.  It still thinks "root" is the root
element name.  And of course DOM doesn't let you directly modify a
DOCTYPE!  So the full solution to support Xerces 1.3 requires changing
the adapter model to set the DOCTYPE at document construction.  That's
something in the TODO already.  I bumped it up higher in the list. 
Alex, this is probably enough to get you going for now.

-jh-



More information about the jdom-interest mailing list