Crear un fichero HTML a partir de un XML

2088 vistas

La creación de un documento HTML con XSLT necesita el uso de una hoja de estilos (XSL).

Veamos un ejemplo:



java
  1. public static void crearHTML(String xml, String xsl, String html) throws Exception{
  2. // Creación del source DOM
  3. DocumentBuilderFactory factoryD = DocumentBuilderFactory.newInstance();
  4. DocumentBuilder constructor = factoryD.newDocumentBuilder();
  5. File fileXml = new File(xml);
  6. Document document = constructor.parse(fileXml);
  7.     Source source = new DOMSource(document);
  8.     // Creación del fichero de salida
  9.     File fileHtml = new File(html);
  10.     Result resultado = new StreamResult(fileHtml);
  11.     // Configuración del transformer
  12.     TransformerFactory factoryT = TransformerFactory.newInstance();
  13.     StreamSource stylesource = new StreamSource(xsl);
  14.     Transformer transformer = factoryT.newTransformer(stylesource);
  15.     transformer.setOutputProperty(OutputKeys.METHOD, "html");
  16.     // Transformación
  17.     transformer.transform(source, resultado);
  18. }