wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Proper use of an EntityResolver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Proper use of an EntityResolver" Watch "Proper use of an EntityResolver" New topic

Proper use of an EntityResolver

Mike Wallace

Joined: Sep 26, 2008
Posts: 3
I have a number of XML documents, loaded from the database, that start with this:

And I am trying to apply an XSL transformation on the given document that I get as a String using this method:

And my CustomEntityResolver looks like this:

In my quest to use an EntityResolver, to avoid resolving the DTD over the network, I came across this method that org.dom4j.Document provides. Its documentation states:

The code as it stands right now throws a file not found exception every time the DTD isn't available on sourceforge, which happens more often than my users would like to see.

Can somebody please explain to me how I am using the EntityResolver incorrectly if I am?

Shouldn't providing my own implementation of an EntityResolver to the Document instance be sufficient to avoid having a transformer go on the network to resolve the DTD?

Thanks a bunch in advance for any and all help anybody can provide.
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

I don't know how dom4j works, but in JAXP you attach the EntityResolver to the parser which is going to parse the document. I'm surprised you can attach an EntityResolver in dom4j to a Document, I would have thought it was the output of a parser. But evidently it isn't.

Is your EntityResolver being called at all? Perhaps your string comparison isn't quite correct.
Yves Zoundi
Ranch Hand

Joined: Aug 31, 2008
Posts: 47
As Paul Clapham, I think that your comparison might be wrong. Try printing the publicid and the systemid all the time to debug.

Author of VFSJFileChooser and XPontus XML Editor
Mike Wallace

Joined: Sep 26, 2008
Posts: 3
Thank you very much for your prompt reply, Paul, and Yves also.

I had initially put a big debug statement outside of the if in the EntityResolver, but as you suspected it isn't getting called at all, so it isn't an incorrect if statement since the code is never getting called.

I was under the impression that perhaps supplying the dom4j Document object itself with an EntityResolver somehow was detected by the Transformer, or the parser the Transormer is using behind the scenes I suppose, and was used to resolve the ID locally.

It makes sense from a theoretical standpoint, but it is surprising as Paul said. What didn't help much was there is not much documentation on what the setEntityResolver method is supposed to do. I wouldn't be shocked if it turns out that I'm just using it incorrectly, but I don't know what else to do. There isn't a way of giving a TransformerFactory or a Transformer your own EntityResolver.

I tried playing with a URIResolver and I supplied that to the TransfomerFactory but that also isn't getting called which makes me think that the underlying implementation, xalan, is simply ignoring resolvers all together.

Is there a better way of applying the transformation needed without depending on a specific implementation?
If not, which implementation do you suggest I use and how?

Thanks again guys.
Yves Zoundi
Ranch Hand

Joined: Aug 31, 2008
Posts: 47
Hello Mike,

The EntityResolver has always worked for me with DOM4J(When using DOM4J with or without JAXP). What I always do is pass the EntityResolver in a SAXReader before creating the DOM4J document:

Mike Wallace

Joined: Sep 26, 2008
Posts: 3
Thank you for replying again, Yves.

I ended up giving up on trying to figure out what the point of dom4j's Document.setEntityResolver method and instead went with a similar approach to what you suggested.

Then I get a Transformer instance through TransformerHandler etc.

I now KNOW that my entity resolver is getting called and that it is functioning properly.

Thanks for all your time and assistance.
[ October 01, 2008: Message edited by: Mike Wallace ]
It is sorta covered in the JavaRanch Style Guide.
subject: Proper use of an EntityResolver
Similar Threads
reading/updating child node attribute in xml String
Servlet + JDOM
File Output stream adding extra character while writing
How do i surpass DTD Entry while Parsing XML
Parsing XML using SAX without DTD