Contents
  Home
  Key Skills & CV
  Links to TecNet Sites
  Sample PL/SQL Code
  Sample JavaScript
  Sample Java Code
  Contact Us
  Help

Problem Solution
How to use the XSL processing capabilities of the Oracle XML Parser V2.0. An input XML document is transformed using a given
input stylesheet like this:
NOTE:
This example works in conjuction with the first pl/sql example.
import org.w3c.dom.*;

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

import oracle.sql.*;

import oracle.xml.parser.v2.*;
import org.xml.sax.InputSource;

public class XMLtoHTML
{
   /**
    * Transforms an xml document using a stylesheet
    */
   public static void convert (String xslfile,
                      CLOB xmlclob,
                      CLOB[] htmlclob) throws Exception
   {
      DOMParser parser;

      XMLDocument xml, xsldoc;

      URL xslURL;

      try
      {
         // Parse xsl and xml documents
         parser = new DOMParser();
         parser.setPreserveWhitespace(true);

         // parser input XSL file
         xslURL = createURL(xslfile);
         parser.parse(xslURL);
         xsldoc = parser.getDocument();

         // parser input XML file
         parser.parse(xmlclob.getCharacterStream());
         xml = parser.getDocument();

         // instantiate a stylesheet
         XSLStylesheet xsl
            = new XSLStylesheet(xsldoc,xslURL);

         XSLProcessor processor = new XSLProcessor();

         // display any warnings that may occur
         processor.showWarnings(true);
         processor.setErrorStream(System.err);

         Writer w = htmlclob[0].getCharacterOutputStream();
         PrintWriter pw = new PrintWriter(w);

         // Process XSL
         processor.processXSL(xsl, xml, pw);
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
   }

   // Helper method to create a URL from a file name
   static URL createURL(String fileName)
   {
      URL url = null;
      try
      {
         url = new URL(fileName);
      }
      catch (MalformedURLException ex)
      {
         File f = new File(fileName);
         try
         {
            String path = f.getAbsolutePath();
      // This is a bunch of weird code that is required to
      // make a valid URL on the Windows platform, due
      // to inconsistencies in what getAbsolutePath returns.
            String fs = System.getProperty("file.separator");
            if (fs.length() == 1)
            {
               char sep = fs.charAt(0);
               if (sep != '/')
                  path = path.replace(sep, '/');
               if (path.charAt(0) != '/')
                  path = '/' + path;
            }
            path = "file://" + path;
            url = new URL(path);
         }
         catch (MalformedURLException e)
         {
            System.out.println("Cannot create url: "+fileName);
            System.exit(0);
         }
      }
      return url;
   }
}
Find certain file types in a specified directory This example finds the .txt file(s) in a directory (dir) passed to it:
File myDir = new File(dir);
String[] fileList = myDir.list (
                    new FilenameFilter() {
                    public boolean accept(File myDir,String s)
                    {return s.endsWith(".txt");}    }
                    );
for (int i = 0; i < fileList.length; i++) {
   file = fileList[i];
}
Read in a file line at a time Start a file reader for the file given a String
file name (thefile) then read file line at a time like this:
String inp

BufferedReader in = new BufferedReader
                        (new FileReader(thefile));

   while (in.ready()) {
      inp = in.readLine();
   }