1

I used JAXB to create a very complicated .xml file which I saved on the drive. I also manually made an .xsl file which is my template.

How do I now programmatically use the above two to create an html output file ?

I tried various things and maybe I'm just tired but I can't even successfully open the .xml file into a Document.

Does someone have a working example ? I would greatly appreciate it! Thanks :)

I tried various things, including the official code examples but I can't find a working example. Nothing but null pointer exceptions. :(

2
  • Did you come across any performance issue ? I have the same kind of code and now I see some performance hit in PROD. Commented Sep 13, 2017 at 17:39
  • stackoverflow.com/questions/46194800/… Commented Sep 13, 2017 at 17:40

2 Answers 2

4

The smallest working example I can give you:

import java.io.File;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class UseXMLToHTML {

    public static void main(String[] args) throws TransformerException {
        StreamResult result = new StreamResult(new File("output.html"));
        StreamSource source = new StreamSource(new File("input.xml"));
        StreamSource xslt = new StreamSource(new File("transform.xslt"));

        Transformer transformer = TransformerFactory.newInstance().newTransformer(xslt);

        transformer.transform(source, result);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot mate, this helped me a great deal. =D
In PROD, I am seeing some Thread getting stuck issues. Any pointers. stackoverflow.com/questions/46194800/…
2

This would probably do the trick;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

public class TestMain {
    public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        Source xslt = new StreamSource(new File("transform.xslt"));
        Transformer transformer = factory.newTransformer(xslt);

        Source text = new StreamSource(new File("input.xml"));
        transformer.transform(text, new StreamResult(new File("output.xml")));
    }
}

Consider trying out stuff from these urls:

http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

http://www.w3schools.com/xsl/tryxslt_result.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

1 Comment

Thank you good sir, not sure which of you posted first but i appreciate the help, i got it working :) Sort off! Lots to do!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.