Academic Work Personal
|
Java /
XML + XSL => HTML
ProblemYou have an XML file with an XSL and you want to combine them to an HTML page. SolutionThe necessary imports: import java.io.InputStream; import java.io.PrintStream; import java.io.StringWriter; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; Here public static void xslTransformToStdout(InputStream xmlIn, InputStream xslIn, PrintStream out) { StreamSource xml = new StreamSource(xmlIn); StreamSource xsl = new StreamSource(xslIn); StreamResult result = new StreamResult(out); TransformerFactory tFactory = TransformerFactory.newInstance(); try { Transformer transformer = tFactory.newTransformer(xsl); transformer.transform(xml, result); } catch (javax.xml.transform.TransformerException te) { System.err.println("Transformer Error in xslTranform: "+te.getMessage()); } } Here the resulting HTML is sent back as a String: public static String xslTransformToString(InputStream xmlIn, InputStream xslIn) { StringWriter sw = new StringWriter(); StreamSource xml = new StreamSource(xmlIn); StreamSource xsl = new StreamSource(xslIn); StreamResult result = new StreamResult(sw); TransformerFactory tFactory = TransformerFactory.newInstance(); try { Transformer transformer = tFactory.newTransformer(xsl); transformer.transform(xml, result); } catch (javax.xml.transform.TransformerException te) { System.err.println("Transformer Error in xslTranform: "+te.getMessage()); } return sw.toString(); } |
![]() anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows Blogs and Dev. * Ubuntu Incident Places Debrecen | France | Hungary | Montreal | Nancy Notes Hobby Projects * Jabba's Codes Quick Links [ edit ] |