Transforming XML - Java XML

Java examples for XML:XML Transform

Introduction

Use the javax.xml.transform package to transform an XML document to another document format.

Demo Code

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

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

public class Main {

  public void run(String xmlFile, String xslFile, String outputFile)
      throws FileNotFoundException, TransformerConfigurationException,
      TransformerException {//from  w ww  . j  a  va2  s.  c om
    InputStream xslInputStream = new FileInputStream(xslFile);
    Source xslSource = new StreamSource(xslInputStream);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(xslSource);
    InputStream xmlInputStream = new FileInputStream(xmlFile);
    StreamSource in = new StreamSource(xmlInputStream);
    StreamResult out = new StreamResult(outputFile);
    transformer.transform(in, out);

  }

  public static void main(String[] args) {
    String fileName = null;
    String fileName2 = null;
    String fileName3 = null;
    fileName = "patients.xml";
    fileName2 = "patients.xsl";
    fileName3 = "patients.html";
    Main app = new Main();
    try {
      app.run(fileName, fileName2, fileName3);
    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
    } catch (TransformerConfigurationException ex) {
      ex.printStackTrace();
    } catch (TransformerException ex) {
      ex.printStackTrace();
    }
  }
}

Result


Related Tutorials