This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Stop Xerces from requesting DTD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Stop Xerces from requesting DTD" Watch "Stop Xerces from requesting DTD" New topic
Author

Stop Xerces from requesting DTD

Don Blodgett
Ranch Hand

Joined: Jan 09, 2006
Posts: 61
I am trying to transform some XML files that have DTDs specified. Unfortunately I do not always have Internet access and so I get exceptions like connection timeout and unknown host. Is there a way to prevent the parser from even attempting to retrieve the DTD? I am using net.sf.saxon.TransformerFactoryImpl to create my transformer instance, any help on how to do this would be greatly appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Nope. Failing to use the DTD could change the meaning of the document, or even make it not well-formed in some cases, so parsers don't give you that option. But there is a way to provide a local version of the DTD to the parser. Check out the setEntityResolver() method.
Don Blodgett
Ranch Hand

Joined: Jan 09, 2006
Posts: 61
I am not able to find any documentation on that method and it does not appear to be a method on the factory transformer instance, of configuration of the factory. Do you know of an example that I could look at?
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 535
    
    3
>it does not appear to be a method on the factory transformer instance, of configuration of the factory
Sure it is not. But is it that difficult to google out what sort of handling it is meant for?

This is a short hand-on:
http://www.ibm.com/developerworks/library/x-tipent/index.html

When you pass on the Source to the transform() method, it can be SAXSource, it can be DOMSource, it can be StreamSource... If you want to have more control on what to pass, you would bet on using SAXSource or DOMSource. To construct them, XMLReader and DocumentBuilder classes can be setup with custom EntityResolver() and that is where you set the re-direction or customizing the handling. In the case you simply want to "kill" the dtd fetching, you can make it return an InputSource without any content at all.

Let x be your XMLReader or DocumentBuilder variable, this is how it is done:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stop Xerces from requesting DTD