By using XSL we can write any mapping to a XML, and we can get any type of conversion by using the javax.xml.transform library to do the requried conversion.
Following java source code helps to do the conversion of XML + XSL to any required type.
public void transform(String xslsource, String profileName, String outputFileName, String xmlsource, String profileXMLPath) {//output file name,input xsl,xml source
try {
String fileName = tomcathome + "/webapps/webclient/WEB-INF/jsp/profiles/" + profileName + "/" + outputFileName;
//output file name. This can be any file name
FileOutputStream fileStream = new FileOutputStream(fileName);
//defining the output steam
InputStream in = this.getClass().getResourceAsStream("/lif/webclient/controller/extract/" + xslsource);
//give the xsl source as an input steam
InputStreamReader xsltStream = new InputStreamReader(in);
Source xsltSource = new StreamSource(xsltStream);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);
//cdxml.xml --> dom tree.
FileInputStream xmlStream = new FileInputStream(profileXMLPath + "/" + xmlsource);
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = documentBuilder.parse(xmlStream);
Source xmlSource = new DOMSource(doc);
Result result = new StreamResult(fileStream);
transformer.transform(xmlSource, result);
fileStream.flush();
fileStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (TransformerConfigurationException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (TransformerException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SAXException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
In above code you need to provide XML file and the mapping XSL file as an InputStream and after supplying both of these steams to transform method in the instance of the TransformFactory and the required output can be gained from the SteamResult. In the SteamResult constructor you can give the required output as an OutputSteam.
Subscribe to:
Post Comments (Atom)
1 comment:
XML to XHTML would be a more suitable topic macahng.
Post a Comment