XStream provides a TraxSource (extends SAXSource) that can be used as an input to XSLT transformation. The TraxSource uses a java Object and the corresponding XStream Object. The java object can then be directly converted to XSLT target without actually converting to XML. Lets look at an example
package com.studytrails.xml.xstream; import java.util.ArrayList; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.TraxSource; public class XStreamTransformationExample { public static void main(String[] args) throws TransformerFactoryConfigurationError, TransformerException { XStreamTransformationExample transformationExample = new XStreamTransformationExample(); transformationExample.runTransformation(); } private void runTransformation() throws TransformerFactoryConfigurationError, TransformerException { XStream xstream = new XStream(); xstream.alias("rss", Rss2.class); xstream.alias("item", Item2.class); Rss2 rss = new Rss2(); Channel2 channel = new Channel2(); rss.channel = channel; channel.title = "Title"; channel.link = "link"; channel.image = new Image2(); channel.image.link = "image link"; Item2 item1 = new Item2(); item1.link = "item link"; item1.title = "Item Title"; channel.items = new ArrayList<Item2>(); channel.items.add(item1); System.out.println(xstream.toXML(rss)); Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("bbc.xsl")); TraxSource traxSource = new TraxSource(rss, xstream); StreamResult result = new StreamResult(System.out); transformer.transform(traxSource, result); // prints the html on console } }
The XML.
Title linkimage link Item Title item link
The Rss2 and other classes
package com.studytrails.xml.xstream; import java.util.Arrays; import java.util.List; public class Rss2 { public Channel2 channel; @Override public String toString() { return "Rss [channel=" + channel + "]"; } } class Channel2 { public String title; public String link; public String description; public String language; public String lastBuildDate; public String copyright; public Image2 image; public String ttl; public AtomLink a_link; public Listitems; public class AtomLink { public String href; public String rel; public String type; @Override public String toString() { return "AtomLink [href=" + href + ", rel=" + rel + ", type=" + type + "]"; } } @Override public String toString() { return "Channel [title=" + title + ", link=" + link + ", description=" + description + ", language=" + language + ", lastBuildDate=" + lastBuildDate + ", copyright=" + copyright + ", image=" + image + ", ttl=" + ttl + ", a_link=" + a_link + ", items=" + items + "]"; } } class Item2 { public String title; public String description; public String link; public String guid; public String pubDate; public MediaThumbnail[] media_thumbnails; @Override public String toString() { return "Item [title=" + title + ", link=" + link + ", guid=" + guid + ", pubDate=" + pubDate + ", media_thumbnails=" + Arrays.toString(media_thumbnails) + "]"; } public class MediaThumbnail { public int width; public int height; public String url; @Override public String toString() { return "MediaThumbnail [width=" + width + ", height=" + height + ", url=" + url + "]"; } } } class Image2 { public String url; public String title; public String link; public String width; public String height; @Override public String toString() { return "Image [url=" + url + ", title=" + title + ", link=" + link + ", width=" + width + ", height=" + height + "]"; } }