public String transform_XML(String type, InputStream file){
TransformerFactory tf = TransformerFactory.newInstance();
String xslfile = "/StyleSheets/" + type + ".xsl";
Transformer t = tf.newTemplates(new StreamSource(this.getClass().getResourceAsStream(xslfile))).newTransformer();
Source source = new StreamSource(file);
CharArrayWriter wr = new CharArrayWriter();
StreamResult result = new StreamResult(wr);
t.transform(source, result);
return wr.toString();
}
The above method takes an xsl and xml file as input and returns the transformed result as String. Classes from Package javax.xml.transform has been used to accomplish this.
Now can i use the same package to transform an html file? (Since the package name has xml i seriously doubt it.) What should i do to transform an html file?
As you understand, html documents aren’t necessary valid xml. But you can transform html to xml, and after that manipulate with valid xml (after transformation – you’ll get DOM tree).
I’d suggest you to use CyberNeko HTML Parser to transform
htmlintoxml.Draft example:
If you use
maven– you can simply add to your projectCyberNekofrom repository http://mvnrepository.com/artifact/nekohtml/nekohtml