Venkat SM Venkat SM - 9 days ago 5
HTML Question

HTML to XML Conversion using XSLT in java

Hi Can anyone help me in html to xml conversion using xslt in java.I converted xml to html using xslt in java.This is the code i used for that converstion:

import javax.xml.transform.*;

import java.net.*;
import java.io.*;

public class HowToXSLT {

public static void main(String[] args) {

try {

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer =
tFactory.newTransformer
(new javax.xml.transform.stream.StreamSource
("howto.xsl"));

transformer.transform
(new javax.xml.transform.stream.StreamSource
("howto.xml"),
new javax.xml.transform.stream.StreamResult
( new FileOutputStream("howto.html")));
}
catch (Exception e) {
e.printStackTrace( );
}
}
}


But i dont know the reverse process of this program that is to convert html to xml? Is there is any jar files available to do that? please help me...

Answer

Generally, it isn't possible to "reverse" a transformation, because a transformation in the general case isn't a 1:1 mapping.

For example, if the transformation does this:

<xsl:value-of select= "/x * /x"/>

and we get as result: 16

(and we know that the source XML document had only one element),

it isn't possible to determine from the value 16 whether the source XML document was:

<x>4</x>

or whether it was:

<x>-4</x>

And the above was only a simple example! :)