How  to output a CLOB to file using UTF-8

I am working on an 9.2.04 database. I'm generating xml using SQLX functions and inserting the result into a table with a column of XMLTYPE datatype.
The database characterset is WE8ISO8859P15. I need to write the contents of my XMLTYPE column to a file on the server using UTF-8 encoding. I've tried The following :
declare
temp_xml xmltype;
cl clob;
begin
select myxml into temp_xml from xml_table;
cl := temp_xml.getClobVal;
clob_to_xmlfile('XMLDIR','myfilename.xml',cl);
end;
The code for clob_to_xmlfile is :
PROCEDURE clob_to_xmlfile( p_dir IN VARCHAR2,
p_file IN VARCHAR2,
p_clob IN CLOB )
AS
l_output utl_file.file_type;
l_amt number default 32000;
l_offset number default 1;
l_length number default nvl(dbms_lob.getlength(p_clob),0);
BEGIN
-- notice use of fopen_nchar and put_nchar to use unicode
l_output := utl_file.fopen_nchar(p_dir, p_file, 'w', 32760);
while ( l_offset < l_length )
loop
utl_file.put_nchar(l_output,
dbms_lob.substr(p_clob,l_amt,l_offset) );
utl_file.fflush(l_output);
l_offset := l_offset + l_amt;
end loop;
utl_file.new_line(l_output);
utl_file.fclose(l_output);
END clob_to_xmlfile;
However, the resultant file has somehow 'lost' characters, thereby making the xml malformed. I know by inspection that the XMLTYPE column in my table contains valid xml, so the 'corruption' is occuring when I try writing it to file.
Is this something to do with characterset s ?? Can anyone help me with this ??

In 10g, DBMS_XMLDOM has writeToFile() writing XML into files without the temporary lobs. It lets you specify a character set parameter. However, 9.2.0.4 doesn't not support character set conversion so this may not work. The options would be upgrading to 10g or building the function like what you have.
The presented code seems to have the followings issues:
(1) the length expansion is not taken into accout; utf-8 encoding needs equal or grater number of bytes to represent the same string in iso-8859-15,
potentially up to 3x. If your buffer was 32k, you should not read more than 10k at a time in order to make sure all characters read from CLOB can fit into the buffer. This problem apparently can cause unexpected behavior: a
symptom may be lost characters.
(2) while ( offset < length ) has a off-by-one problem, as we start counting LOB offset with 1, not zero.
(3) I think utl_file.fflush needs the new line character at the end of buffer so it can tell how much to write. It would be difficult to write a bufferful of data because you have no idea where a line begins and ends
while reading a CLOB.
Probably a more practical approach to make this work would be using the PUT_RAW function with autoflush turned on, where data can be written blindly regardless of when the buffer should be flushed. That is:
-read 10k from the clob (DBMS_LOB.READ)
-manually convert it into UTF-8 to a 30k buffer (UTL_RAW.CONVERT)
-then write it with UTL_FILE.PUT_RAW (UTL_RAW.CAST_TO_RAW)

Similar Messages

  • How to write a CLOB to file using UTF-8

    I'm new to java and I have the following problem.
    I need to write a CLOB to file using the UTF-8 characterset (I'm generating an xml file). My database characterset is WE8ISO8859P15.
    I've tried the code below but the resulting output fails validation in an xml editor due to an 'invalid character' error :
    create or replace
    and compile
    java source named "ClobUtil"
    as
    import java.io.*;
    import java.lang.*;
    import java.sql.*;
    import java.util.*;
    import oracle.jdbc.driver.*;
    import oracle.sql.*;
    public class ClobUtil {
    public static void save(CLOB clob, String filename) throws Exception
    Connection conn = DriverManager.getConnection("jdbc:oracle:kprb:");
    conn.setAutoCommit(false);
    InputStream is = clob.getCharacterStream();
    FileOutputStream os = new FileOutputStream(filename);
    int size = clob.getChunkSize();
    byte buffer[] = new byte[size];
    int length;
    while ((length = is.read(buffer, 0, size)) != -1) {
    os.write(buffer, 0, length);
    is.close();
    os.close();
    conn.close();
    I see that the getCharacterStream() method returns a Unicode character stream but how do I output this to file ??
    Thanks in advance

    File file = new File( "myfile.utf8" );
    FileOutputStream fos = new FileOutputStream( file );
    OutputStreamWriter osw = new
    OutputStreamWriter( fos, "UTF8" );
    Writer w = new BufferedWriter( osw );
    PrintWriter out = new PrintWriter( w );
    Or whatever you need. Note that this merely allows you to specify the encoding to be used in the output file, it does convert the encoding for you. I think in your case this will work since you already know the output stream will not contain the full range of UTF8 characters anyway.

  • How to output data to a file in SCC-SG04?

    I am using SCC-2345 with SCC-SG04 connected to NI-6221 in Windows 2000 in VC6.
    How to output data to a file like data1.dat in VC6 or in Labwindows/CVI?

    Hello mwibm,
    If you just want to do file input/output in LabWindows/CVI, I would take a look at the Formatting and I/O CVI library or the ANSI File I/O functions. For example, if you just want to write an array of data to a file, I would look at the ArrayToFile function or the fwrite/fputs/fprintf ANSI C functions included in stdio.h. The ANSI C functions will also work in Visual Studio. More information on the LabWindows/CVI File I/O functions can be found in CVI help, and more information on ANSI C functions can be found in CVI Help and online at various websites.
    Maybe you could further clarify what problems you are having and what kind of data you want to write to a file.
    Thanks.
    Wendy L
    LabWindows/CVI Developer Newsletter - ni.com/cvinews

  • How do I produce an excel file using java code

    How do i produce an excel file using java code.
    If it is possible would ne one have ne source code as an example.
    I have tried looking up information on outputing the information to a *.csv file but unfortunality i have had no luck in finding any information. To be exact ne source code.
    Thanks
    Lee

    That's what I use and I'd be the first to admit that it's very messy. But it works. Go use Google and see what else you can find.

  • How do I edit a PDF file using Acrobat 9 - Version 9.4.0

    My company - [name deleted by host] purchased Acrobat 9 - Version 9.4.0. for me
    How do I edit a PDF file using Acrobat 9 - Version 9.4.0
    I need to edit - copy, extract text, add text, etc. from existing PDF files to SAVE my changes,
    or create a new PDF file from an existing file.
    This is very important for me to be able to do my job effectively.
    I spent over 2 hrs the last few days, and another hour today, still can't do it.
    PLEASE respond ASAP with a solution.
    Thanks in advance,
    - Daulton West
    [signature deleted by host]

    PDF is an end destination format - although Acrobat is capable of making minor changes to a file (e.g. to correct typos) it is not a word processor or DTP application. Major changes must be done in the original document, and the PDF file re-exported.
    You can copy text from a file by selecting it and right-clicking, or you can save the file as plain or formatted text. To make small changes to the text on a page, use the touchup tool - see the help file for more info.

  • How do I open a .dd file using OS X?

    I had a corrupt external HDD which i used to use with an old laptop. I used data recovery software and managed to recover 33gb of data. However it is saved in the old diskdoubler format. (.dd file extension). How do I open a .dd file using OS X? ive tried stuffit and unarchiver but they dont work. any help?

    Look at these links.
    http://forums.appleinsider.com/archive/index.php/t-40241.html
    http://www.enews.org/blog/2009/03/opening-diskdoubler-files-from-mac-os-9.html
     Cheers, Tom

  • How do I generate the pdf file using the name of a field.  How can I help

    how do I generate the pdf file using the name of a field.  How can I help

    Hi,
    here's a sample.
    LiveCycle Blog: Formulare in bestimmte Verzeichnisse speichern und nach Inhalt aus Formularfeld benennen //Save forms to…

  • How to write data to text file using external tables

    can anybody tell how to write data to text file using external tables concept?

    Hi,
    Using external table u can load the data in your local table in database,
    then using your local db table and UTL_FILE pacakge u can wrrite data to text file
    external table
    ~~~~~~~~~~~
    http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_7002.htm#i2153251
    UTL_FILE
    ~~~~~~~~~
    http://download-east.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_file.htm#sthref14093
    Message was edited by:
    Nicloei W
    Message was edited by:
    Nicloei W

  • How to create and read text file using LabVIEW 7.1 PDA module?

    How to create and read text file using LabVIEW 7.1 PDA module? I can not create a text file and read it.
    I attach my code here.
    Attachments:
    File_IO.vi ‏82 KB

    Well my acquisition code runs perfect. The problem is reading it. I can't seem to read my data no matter what I do. My data gets saved as a string using the array to string vi but I've read that the string to array vi (which I need to convert back to array to read my data) does not work on the pda. I'm using version 8.0. So I was trying to modify the program posted in this discussion so that it would save data from my DAQ. I did that but I still can't read the data after its saved. I really don't know what else to do. All I need to do is read the data on the pda itself. I can't understand why I'm having such a hard time doing that. I found a possible solution on another discussion that talks about parsing the strings because of the bug in the "string to array" vi. However, that lead me to another problem because for some reason, the array indicators or graphs don't function on the pda. When i build the program to the pda or emulator, the array indicators are faded out on the front panel as if the function is not valid. Does this kind of help give a better picture of what I'm trying to do. Simply read data back. Thanks.

  • How get output generated as csv file  by reading  by buffered reader and wr

    how get output generated as csv file by reading by buffered reader and writer

    String file_location = "C\temp\csv.txt");
    try {
         URL fileURL = getClass().getResource(file_location);
         if (fileURL != null){
              BufferedReader br = new BufferedReader(new InputStreamReader(fileURL.openStream()));
              String s = br.readLine();
              while (s != null)  {
                   if (!s.equals ("")) {
                        System.out.println(s);
                   s = br.readLine();
              br.close();
         else {
              // error
    catch (IOException ex){ex.printStackTrace();}rykk
    Message was edited by: a dummy
    rykk.

  • How can i rename a jar file using only java code

    i have tried everything i can think of to accomplish this but nothing works.

    ghostbust555 wrote:
    In case you geniuses haven't realized I said I tried everything I can think of not that I tried everything. So help or shut up I realize that I didn't try everything but if you can't figure it out either DO NOT POST.
    And the question is how can i rename a jar file using java code? As it says in the title. Read.I would tell you to use the File.renameTo method, but surely that would have been obvious, and you would have tried it already? But maybe you didn't. You were kind of lacking in details in what you tried.
    And yes, I am a genius. Just don't confuse "genius" with "mind-reader".

  • How to access the database jar file using the derby 10.2.1.6 database ?

    Hi,
    How to access the database jar file using the derby 10.2.1.6 database ?
    I have used like below. And i am getting the following the error:
    "org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'org.apache.derby.jdbc.EmbeddedDriver'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1136)"
    My context.xml file looks like this:
    <Context crossContext="true">
    <Resource name="jdbc/derby" auth="Container"
    type="javax.sql.DataSource" driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
    url="jdbc:derby:jar(\CalypsoDemo\database.jar)samples"
    username="xxx" password="xxx" maxActive="20" maxIdle="10"
    maxWait="-1"/>
    </Context>
    What could be the reason.?
    Any suggestions will be appriciated.
    Thanks in Advance,
    Gana.

    ya, I have restarted. Can you please tell me whether the path which i am giving is right or not in the context file?
    Thanks,
    Gana.

  • How to create a new excel file using Excel Destination when Destination file not exists.

    how to create a new excel file using Excel Destination when Destination file not exists.

    Just need to set an expression for excel connectionstring and set delay validation to true and it will create it on the fly.
    The expression should return the full path with dynamic filename in each case.
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to output to an excel file?

    Hi,
    I have written a program that gets data from the database and I like to find out how to output the data to an excel file. I have read about similar topic on the discussion board and the solution are for the online application. Mine is a desktop application. The user can specify what the destination file is. Can anyone help. Thanks.

    The simpliest way is to take your data and write it to a Comma Dilimeted File which Excel can read without any problems or user intervention. You would use an ObjectOutputStream. Hope this helps.

  • How to output results to a file?

    If there is no Debug capability, how to output test results to a file?  Are there any other ways to do this?
    Thanks!

    Dear Sapport,
    You can use the IWDMessageManger to print the message on the webdynpro application iview screen.
    Please refer to the more details for the Webdynpro Logging and Tracing to find the root cause:-
    [Logging in Web Dynpro (Java) - a guide and tutorial for beginners [Part 1]|Logging in Web Dynpro (Java) - a guide and tutorial for beginners [Part 1]]
    [http://wiki.sdn.sap.com/wiki/display/WDJava/LoggingandTracinginWebDynpro|http://wiki.sdn.sap.com/wiki/display/WDJava/LoggingandTracinginWebDynpro]
    Hope it will resolve your issue.
    Best Regards
    Arun Jaiswal
    Edited by: Arun Jaiswal on Mar 5, 2012 12:47 PM

Maybe you are looking for