Show me the code! – By Davanum Srinivas

July 9, 2008

Input XML, XSL in a String, output in a String

Filed under: Uncategorized — Davanum Srinivas @ 1:06 pm

Here you go ken!

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;

public class XML {
    static String xmlIn = "<?xml version=\"1.0\"?>\n" +
            "<?xml-stylesheet type=\"text/xml\" href=\"17-2.xsl\"?>\n" +
            "<PERIODIC_TABLE>\n" +
            "  <ATOM STATE=\"GAS\">\n" +
            "    <NAME>Hydrogen</NAME>\n" +
            "    <SYMBOL>H</SYMBOL>\n" +
            "    <ATOMIC_NUMBER>1</ATOMIC_NUMBER>\n" +
            "    <ATOMIC_WEIGHT>1.00794</ATOMIC_WEIGHT>\n" +
            "    <BOILING_POINT UNITS=\"Kelvin\">20.28</BOILING_POINT>\n" +
            "    <MELTING_POINT UNITS=\"Kelvin\">13.81</MELTING_POINT>\n" +
            "    <DENSITY UNITS=\"grams/cubic centimeter\">\n" +
            "      <!-- At 300K, 1 atm -->\n" +
            "      0.0000899\n" +
            "    </DENSITY>\n" +
            "  </ATOM>\n" +
            "  <ATOM STATE=\"GAS\">\n" +
            "    <NAME>Helium</NAME>\n" +
            "    <SYMBOL>He</SYMBOL>\n" +
            "    <ATOMIC_NUMBER>2</ATOMIC_NUMBER>\n" +
            "    <ATOMIC_WEIGHT>4.0026</ATOMIC_WEIGHT>\n" +
            "    <BOILING_POINT UNITS=\"Kelvin\">4.216</BOILING_POINT>\n" +
            "    <MELTING_POINT UNITS=\"Kelvin\">0.95</MELTING_POINT>\n" +
            "    <DENSITY UNITS=\"grams/cubic centimeter\"><!-- At 300K -->\n" +
            "      0.0001785\n" +
            "    </DENSITY>\n" +
            "  </ATOM>\n" +
            "</PERIODIC_TABLE>";
    static String xsl = "<?xml version=\"1.0\"?>\n" +
            "<xsl:stylesheet version=\"1.0\" \n" +
            "          xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +
            "  <xsl:template match=\"PERIODIC_TABLE\">\n" +
            "    <html>\n" +
            "      <xsl:apply-templates/>\n" +
            "    </html>\n" +
            "  </xsl:template>\n" +
            "  <xsl:template match=\"ATOM\">\n" +
            "    <P>\n" +
            "      <xsl:apply-templates/>\n" +
            "    </P>\n" +
            "  </xsl:template>\n" +
            "</xsl:stylesheet>";
    public static void main(String[] args) throws Exception {
        StreamSource xslSource = new StreamSource(new StringReader(xsl));
        StreamSource xmlInSource = new StreamSource(new StringReader(xmlIn));
        Transformer tf = TransformerFactory.newInstance().newTransformer(xslSource);
        StringWriter xmlOutWriter = new StringWriter();
        tf.transform(xmlInSource, new StreamResult(xmlOutWriter));
        System.out.println(xmlOutWriter.toString());
    }
}

XML and XSL from

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: