Java DOM Parser XML and XSLT examples
With XSLT(Extensible Stylesheet Language Transformations), we can transform XML documents into other formats such as HTML.
Table of contents
- 1. DOM Parser and Transformer
- 2. DOM example: XML + XSLT = HTML format
- 3. Download Source Code
- 4. References
P.S Tested with Java 11
1. DOM Parser and Transformer
We can use the TransformerFactory
to apply the XSLT
file to transform an XML file to another format.
TransformerFactory transformerFactory = TransformerFactory.newInstance();
// add XSLT in Transformer
Transformer transformer = transformerFactory.newTransformer(
new StreamSource(new File("format.xslt")));
transformer.transform(new DOMSource(doc), new StreamResult(output));
2. DOM example: XML + XSLT = HTML format
The below is a Java DOM parser example to parse an XML file, apply an XSLT file and transform the XML file into an HTML file.
2.1 An XML file.
staff-simple.xml
<?xml version="1.0" encoding="utf-8"?>
<company>
<staff id="1001">
<name>mkyong</name>
<role>support</role>
</staff>
<staff id="1002">
<name>yflow</name>
<role>admin</role>
</staff>
</company>
2.2 Apply below XSLT file will transform the XML file into an HTML format.
staff-xml-html.xslt
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-size:12pt;background-color:#EEEEEE">
<h1 style="font-size:20pt;color:#FF0000">Hello World DOM + XML + XSLT</h1>
<xsl:for-each select="company/staff">
<ul>
<li>
<xsl:value-of select="@id"/> -
<xsl:value-of select="name"/> -
<xsl:value-of select="role"/>
</li>
</ul>
</xsl:for-each>
</body>
</html>
2.3 Output, an HTML file.
<html>
<body style="font-size:12pt;background-color:#EEEEEE">
<h1 style="font-size:20pt;color:#FF0000">Hello World DOM + XML + XSLT</h1>
<ul>
<li>1001 -
mkyong -
support</li>
</ul>
<ul>
<li>1002 -
yflow -
admin</li>
</ul>
</body>
</html>
2.4 The below is the Java DOM parser to do the XSLT transformation.
XsltXmlToHtmlDomParser.java
package com.mkyong.xml.dom;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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 java.io.*;
// XML -> XSLT -> Other formats
public class XsltXmlToHtmlDomParser{
private static final String XML_FILENAME
= "src/main/resources/staff-simple.xml";
private static final String XSLT_FILENAME
= "src/main/resources/xslt/staff-xml-html.xslt";
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try (InputStream is = new FileInputStream(XML_FILENAME)) {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
// transform xml to html via a xslt file
try (FileOutputStream output =
new FileOutputStream("c:\\test\\staff.html")) {
transform(doc, output);
}
} catch (IOException | ParserConfigurationException |
SAXException | TransformerException e) {
e.printStackTrace();
}
}
private static void transform(Document doc, OutputStream output)
throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
// add XSLT in Transformer
Transformer transformer = transformerFactory.newTransformer(
new StreamSource(new File(XSLT_FILENAME)));
transformer.transform(new DOMSource(doc), new StreamResult(output));
}
}
Output – c:\\test\\staff.html
3. Download Source Code
$ git clone https://github.com/mkyong/core-java
$ cd java-xml
$ cd src/main/java/com/mkyong/xml/dom/xslt/
Hey,
Your articles are awesome !
Keep going !
thank you for the great content