4 Replies Latest reply on Mar 23, 2007 7:21 PM by DrClap

    How to convert xslt file into string

    807606
      i'm writting a java program to use xslt to transform the xml file. i'm encountering the problem when i try to convert the xslt file into string. i've defined my utility class called 'XmlUtil' to carry out the operation of transform xml file through xslt. but in my main java program i need to convert both xml and xslt file into a string in order to input them in my function argument. my function argument is as follows:
      String htmlString = XmlUtil.applyXsltString(xmlContent, xsltString);

      i've already converted xmlcontent into string by using:
      xmlContent = xmlContentBuffer.toString();

      but i don't know how to convert 'xsltString' now ? i've searched the google for an hour but i cannot find the solution. anyone can help ?

      detail of my souce code is as follow:
      import java.io.*;
      import java.net.*;
      import java.lang.*;
      import java.io.StringReader;
      import java.lang.reflect.Array;
      import java.util.Properties;

      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;

      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.sax.SAXResult;
      import javax.xml.transform.sax.SAXSource;
      import javax.xml.transform.sax.SAXTransformerFactory;
      import javax.xml.transform.sax.TransformerHandler;
      import javax.xml.transform.stream.StreamSource;

      import org.apache.xml.serializer.OutputPropertiesFactory;
      import org.apache.xml.serializer.Serializer;
      import org.apache.xml.serializer.SerializerFactory;

      import org.w3c.dom.Document;
      import org.w3c.dom.Node;
      import org.w3c.dom.NodeList;

      import org.xml.sax.InputSource;
      import org.xml.sax.XMLReader;
      import org.xml.sax.helpers.XMLReaderFactory;

      import XmlUtil;

      public class FileDownload {
      public static void download(String address, String localFileName){
      OutputStream out = null;
      URLConnection conn = null;
      InputStream in = null;
      StringBuffer xmlContentBuffer = new StringBuffer();
      String temp = new String();
      String xmlContent;
      try {
      URL url = new URL(address);
      out = new BufferedOutputStream(
      new FileOutputStream(localFileName));
      conn = url.openConnection();
      in = conn.getInputStream();
      byte[] buffer = new byte[1024];
      int numRead;
      long numWritten = 0;
      System.out.println (in.toString ());
      while ((numRead = in.read(buffer)) != -1) {
      out.write(buffer, 0, numRead);
      numWritten += numRead;
      temp = new String(buffer);
      xmlContentBuffer.append(temp);
      }
      System.out.println(localFileName + "\t" + numWritten);
      xmlContent = xmlContentBuffer.toString();

      String htmlString = XmlUtil.applyXsltString(xmlContent, xsltString);
      } catch (Exception exception) {
      exception.printStackTrace();
      } finally {
      try {
      if (in != null) {
      in.close();
      }
      if (out != null) {
      out.close();
      }
      } catch (IOException ioe) {
      }
      }
      }
      public static void download(String address) {
      int lastSlashIndex = address.lastIndexOf('/');
      if (lastSlashIndex >= 0 &&
      lastSlashIndex < address.length() - 1) {
      download(address, address.substring(lastSlashIndex + 1));
      } else {
      System.err.println("Could not figure out local file name for " + address);
      }
      }
      public static void main(String[] args) {
      for (int i = 0; i < args.length; i++) {
      download(args);
      }
      }

      }
        • 1. Re: How to convert xslt file into string
          807606
          I don't understand why you need load the XML and XLS files into a String. A Transformer can be constructed from a Source and there is a StreamSouce which can be constructed from an InputStream. The transform() method can take a Source as input and can produce a Result. There is no need to go near a String representation of either the input.
          • 2. Re: How to convert xslt file into string
            DrClap
            Agreed. And besides, the cumbersome procedure you have there for reading the file into a String ignores the encoding of the XML document. This could cause problems. You have access to XML-processing classes that handle that properly. You should use them.
            • 3. Re: How to convert xslt file into string
              807606
              en, i think i just want to try whether this way works or not. honestly, i haven't thought of encoding of xml document problem yet. but at this stage i just want to see whether i convert both of them into string file works or not. can anyone tell me how to convert the xslt file into string ?
              • 4. Re: How to convert xslt file into string
                DrClap
                I don't see why it's so important to you to do the wrong thing. If this XMLUtil thing is forcing you into using Strings, you should stop using it.

                You would read the XSLT file into a String in exactly the same way you read the XML file into a String, wouldn't you?