Interfacing the Oracle XSLT Processor to Java

Interfacing the Oracle XSLT Processor to Java

It takes a little more effort to perform an XSLT transformation using the Oracle XSLT processors API. Here I show how it works in a new example, oraclejava.java.

First in oraclejava.java, you must read in the documents you want to work with, using a DOMParser object:

 import org.w3c.dom.*;  import java.util.*;  import java.io.*;  import java.net.*;  import oracle.xml.parser.v2.*;  public class oraclejava  {     public static void main (String args[]) throws Exception      {         DOMParser parser;          try          {             parser = new DOMParser();              parser.setPreserveWhitespace(true);          .          .          . 

Next, you convert the URLs of the XML source and XSLT stylesheet documents into Java URL objects, using the parser objects parse method to read those documents in. Then I use the parsers getDocument method to retrieve and store the XML and XSLT documents in XMLDocument objects:

 public class oraclejava  {     public static void main (String args[]) throws Exception      {         DOMParser parser;          XMLDocument xmldoc, xsldoc;          URL xslURL;          URL xmlURL;          try          {             parser = new DOMParser();              parser.setPreserveWhitespace(true);              xmlURL = new URL(args[0]);              parser.parse(xmlURL);              xmldoc = parser.getDocument();              xslURL = new URL(args[1]);              parser.parse(xslURL);              xsldoc = parser.getDocument();          .          .          . 

At this point, planets.xml and planets.xsl are both in XMLDocument objects. To perform the transformation, I need an XSLStylesheet object and an XSLProcessor object built for the XSLT stylesheet. The actual XSLT transformation is made with the parser objects processXSL method, which returns a document fragment:

 public class oraclejava  {     public static void main (String args[]) throws Exception      {         DOMParser parser;          .          .          .              xslURL = new URL(args[1]);              parser.parse(xslURL);              xsldoc = parser.getDocument();              XSLStylesheet xslstylesheet = new XSLStylesheet(xsldoc, xslURL);              XSLProcessor processor = new XSLProcessor();              DocumentFragment docfragment = processor.processXSL(xslstylesheet, xmldoc);              .              .              . 

That completes the transformation. Now the trick is to convert this document fragment into an XML document that can be written to disk. I do that by creating a new XML document, newdoc , and making the document fragment into the new documents root:

 import org.w3c.dom.*;          .          .          .  public class oraclejava  {     public static void main (String args[]) throws Exception      {         DOMParser parser;          XMLDocument xmldoc, xsldoc, newdoc;          URL xslURL;          URL xmlURL;          try          {             .              .              .              DocumentFragment docfragment = processor.processXSL(xslstylesheet, xmldoc);              newdoc = new XMLDocument();              Element rootElement = newdoc.createElement("root");              newdoc.appendChild(rootElement);              rootElement.appendChild(docfragment);              .              .              . 

All that remains is to store the new XML document to disk, using the name given to us as args[2] . I do that with a FileOutputStream object; heres the full code:

Listing 10.9 oraclejava.java, Interfacing the Oracle XSLT Processor to Java
 import org.w3c.dom.*;  import java.util.*;  import java.io.*;  import java.net.*;  import oracle.xml.parser.v2.*;  public class oraclejava  {     public static void main (String args[]) throws Exception      {         DOMParser parser;          XMLDocument xmldoc, xsldoc, newdoc;          URL xslURL;          URL xmlURL;          try          {             parser = new DOMParser();              parser.setPreserveWhitespace(true);              xmlURL = new URL(args[0]);              parser.parse(xmlURL);              xmldoc = parser.getDocument();              xslURL = new URL(args[1]);              parser.parse(xslURL);              xsldoc = parser.getDocument();              XSLStylesheet xslstylesheet = new XSLStylesheet(xsldoc, xslURL);              XSLProcessor processor = new XSLProcessor();              DocumentFragment docfragment = processor.processXSL(xslstylesheet, xmldoc);              newdoc = new XMLDocument();              Element rootElement = newdoc.createElement("root");              newdoc.appendChild(rootElement);              rootElement.appendChild(docfragment);              OutputStream out = new FileOutputStream(args[2]);              newdoc.print(out);              out.close();          }          catch (Exception e){}      }  } 

And that completes oraclejava.java. To compile this example, set the classpath to include the Oracle XSLT processors XSLT processor, xmlparserv2.jar:

 C:\>set classpath=.;c:\oraclexml\lib\xmlparserv2.jar 

Then compile oraclejava.java as I already did with other Java files, using the Java compiler, javac. To perform XSLT transformations, you supply the URLs of the documents you want to work with (you can use file URLs here, as before, if the documents are local):

 C:\>java oraclejava http://starpowder.com/planets.xml http://starpowder.com/planets.xsl graphics/ccc.gif planets.html 


Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net