Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1226
  • Last Modified:

Exception using Java and XSLT to parse XHTML input file

I am using the following application:

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class Transform {

     * Performs an XSLT transformation, sending the results
     * to System.out.
    public static void main(String[] args) {
        if (args.length != 3) {
                "Usage: java Transform [xmlfile] [xsltfile]");

        File xmlFile = new File(args[0]);
        File xsltFile = new File(args[1]);
        File xmlDest = new File(args[2]);

        // JAXP reads data using the Source interface
        Source xmlSource = new StreamSource(xmlFile);
        Source xsltSource = new StreamSource(xsltFile);

        // the factory pattern supports different XSLT processors
        TransformerFactory transFact =
        Transformer trans = transFact.newTransformer(xsltSource);
        trans.transform(xmlSource, new StreamResult(xmlDest));
      catch (TransformerException e2) {
           System.out.println("Transformer Exception cause = " + e2);    
      catch (Exception e) {
           System.out.println("Exception ==== " + e);  

to parse an XHTML input file with a DOCTYPE tag as the first tag as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

I call the application from the command line as follows:
java Transform input.xml stylesheet.xsl output.xml

It throws the following exception:
ERROR:  'Connection timed out: connect'
ERROR:  'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Connection timed out: connect'
Transformer Exception cause = javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Connection timed out: connect

Please help,
1 Solution
Geert BormansCommented:
Hi Dave,

> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

do you need validation, or is wellformedness checking OK?
Does the machine you are using for this validation have a clear internet connection?

The thing is, since the public reference is likely unknown to the parser
the parser will try to find the system reference,
which is on the W3C website
(browsers do it differently, XML parsers will try to catch the DTD)

now finding the DTD is not an issue, resolving all the referenced DTD modules might be an issue
   "-//W3C//ENTITIES Latin 1 for XHTML//EN"

If you really need validation,
I suggest you keep a local copy of the DTD, with all the modules paths set correctly

Since you just want to do a transform,
it is likely that you don't need validation
so you might be better off removing the first two lines (DOCTYPE)

If you can't remove the DOCTYPE from the file(s) easily,
try to find a way to switch off validation, I don't know how to do that in a java transform


Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now