Source for Transforming DOM Node to HTML with JAXP : Transformer « XML « Java Tutorial






import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;

public class JAXPTransformNode {

  public static void main(String args[]) throws Exception, TransformerException,
      FileNotFoundException {

    TransformerFactory factory = TransformerFactory.newInstance();

    DOMSource stylesheet = new DOMSource(buildDoc(args[1]));
    StreamSource xmlDoc = new StreamSource(args[0]);
    StreamResult result = new StreamResult(new FileOutputStream(args[2]));

    Transformer transFormer = factory.newTransformer(stylesheet);

    transFormer.transform(xmlDoc, result);

  }

  public static Document buildDoc(String document) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder db = dbf.newDocumentBuilder();

    Document theDocument = db.parse(new File(document));

    return theDocument;

  }
}








33.11.Transformer
33.11.1.Source for the JAXP Transformation Application
33.11.2.Source for Transforming DOM Node to HTML with JAXP
33.11.3.Catch TransformerException
33.11.4.Transforming an XML File with XSL into a DOM Document
33.11.5.Applying XSLT Stylesheets
33.11.6.Create an XML file and attach an XSL
33.11.7.And now to attach an XSL
33.11.8.Formatting an XML file using Transformer