Search This Blog

Total Pageviews

XML to HTML Converter

import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

 *Java class to transform XML to HTML.
public class XSLTransformer
    public static void main(String[] str) throws Exception {        
        XSLTransformer xslt = new XSLTransformer() ;
        String output = xslt.transform() ;
        System.out.println( output) ;        

    public String transform(String sourceinput, String xslinput)
            throws Exception {
        Source source = new StreamSource(new StringReader(sourceinput));
        Source xsl = new StreamSource(new StringReader(xslinput));
        Writer outWriter = new StringWriter();
        Result result = new StreamResult(outWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(xsl);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        String outString = outWriter.toString();
        return outString;

    public String transform() throws Exception {
        String sourceStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Company>  <Employee>  <name Java1=\"J1\">DevCommunity.net  </name>  </Employee>  <Employee>  <name C1=\"C1\">C# developer  </name>  </Employee>  <Employee>  <name Perl1=\"p1\">Java developer </name>  </Employee></Company> ";
        String xslStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"html\" indent=\"yes\"/>  <xsl:template match=\"/\">  <html>  <head>  <title>Java Developer</title>  </head>  <body>  <xsl:apply-templates/>  </body>  </html>  </xsl:template>  <xsl:template match=\"devcommunity\">  <xsl:value-of select=\"@key\"/>=  <xsl:value-of select=\"@value\"/>  <br></br>  </xsl:template></xsl:stylesheet>";
        return transform(sourceStr, xslStr);

No comments:

Saga Design Pattern

The Saga design pattern is a way to manage distributed transactions in a microservices architecture. In a microservices environment, where d...