Monday, November 22, 2010

XmlToHtmlConverter

import java.io.FileOutputStream;

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.StreamSource;
import javax.xml.transform.stream.StreamResult;

public class XmlToHtmlConverter {

public static void main(String[] args) {
try {

String xmlFile = "src/xml/xml.xml";
String xsltFile = "src/xslt/xslt.xsl";
String outFile = "src/html/html.html";

Source xmlSource = new StreamSource(xmlFile);
Source xsltSource = new StreamSource(xsltFile);
Result ouput = new StreamResult(new FileOutputStream(outFile));

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xsltSource);
transformer.transform(xmlSource, ouput);

System.out.println("Html got generated");

} catch (Exception e) {
e.printStackTrace();
}
}
}


http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

from this URL download the xml and xsl and try to give input to the above java file..

you will be getting the corresponding html.

No comments:

Post a Comment