Converting elements from a SAX stream to JDOM elements

Colin Horne <[email protected]>
Newsgroups gmane.comp.java.jdom.general
Message-ID <[email protected]>
Hello,

I have a long (infinite) XML stream, which I intend to parse with SAX.
Each individual element in the stream is small, and should be parsed
with JDOM:

<stream>
<element>...</element>
<element>...</element>
</stream>

So each <element> (and their children) are parsed with JDOM, but the
<stream> as a whole is parsed with SAX. It would be preferable if each
<element> does not have to be serialized to an encoded string, and if
elements are not processed twice (e.g., using SAX to echo the
<element>'s XML to a stream, which is then read by JDOM).

I've found several references to this problem from the past, but could
not find a complete solution.

My initial approach was to use the SAXHandler, like so:

        jdomHandler = new SAXHandler() {
            public void endElement(String uri, String localName, String qName) {
                if (qName.equals("an element which I want JDOM to parse")) {
                    // change the SAX handler to myHandler
                } else {
                    super.endElement(uri, localName, qName);
                }
            }
        };


        myHandler = new DefaultHandler() {
            public void startElement(String uri, String localName,
String qName, Attributes attributes) {
                if (qName.equals("an element which I want JDOM to parse")) {
                    // change the SAX handler to jdomHandler
                }
            }
        };


(Ignoring for now that the endElement() method needs to keep track of
its nesting level)

However, trivially doing the above does not work, and fails after
calling jdomHandler.getDocument():

Exception in thread "main" java.lang.IllegalStateException: Root element not set
	at org.jdom.Document.getRootElement(Document.java:218)


I've looked at the initalization code that JDOM is normally doing in
the SAXBuilder.build() method, and am reluctant to copy/modify the
code, because I suspect it will break with future releases, and can't
help but wonder if it would be over-complicating things.

Is there a Right Way(TM) to do this? If so, I might also suggest that
it's referenced from the FAQ.

Many thanks,
  Colin
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.