I need to store the text of an XML file that I already have stored in a location of an FTP, in a field of a table in the database, I am using the package UTL_FILE
to read files, I am not very clear about the operation of the package and I just start with the topic of procedures in PL/SQL. My approach to date is to:
- Use
UTL_FILE.FOPEN
to open the file. - Create an
INSERT
inside procedure to store in a database field
For this I am performing a procedure in PL/SQL I do not know if it is the right thing for what I am requiring
DECLARE
file BFILE;
clob CLOB;
FILE UTL_FILE.file_type;
TEXT VARCHAR2(32767);
L_CONN UTL_TCP.connection;
CODE SPRCMNT.MNTCODE%TYPE;
TEXT MNT.MNT_TEXT%TYPE;
TEXT_NAR MNT.MNT_TEXT_NAR%TYPE;
BEGIN
L_CONN := FTP.LOGIN('000.00.000.00', '00', '*********', '*********');
FTP.ASCII(P_CONN => L_CONN);
FTP.GET(P_CONN => L_CONN, p_from_file => '/xml_file/file.xml', p_to_dir => 'UPLOAD_DIR', p_to_file => 'file.xml');
ftp.logout(l_conn);
VFILE := UTL_FILE.fopen('UPLOAD_DIR', 'file.xml', 'R', 4000);
LOOP
BEGIN
UTL_FILE.GET_LINE(VFILE, TEXT, 32767);
DBMS_OUTPUT.PUT_LINE(TEXT);
INSERT INTO SPRETA (
ID,
CODE,
TEXT,
TEXT_NAR,
DATE)
VALUES('15218',
'15',
TEXT,
TEXT_NAR,
SYSDATE)
RETURN TEXT_NAR INTO l_clob;
l_bfile := BFILENAME ('UPLOAD', 'file.xml');
dbms_lob.fileopen( l_bfile, dbms_lob.FILE_READONLY );
dbms_lob.loadfromfile( l_clob, l_bfile, dbms_lob.getlength(l_bfile) );
dbms_lob.fileclose( l_bfile );
COMMIT;
EXCEPTION
WHEN OTHERS THEN EXIT;
dbms_output.put_line('Error al cargar el archivo');
END;
END LOOP;
UTL_FILE.FCLOSE(FILE);
END;
Where it is CMTT_CODE
going to save by default the value 15, TEXT
save the name of the XML file and TEXT_NAR
all the content, I mean all the XML text in the table MNT
I still can't insert all the XML text in TEXT_NAR
the table field MNT
.
I am trying to adhere to my keep it simple philosophy but I am new to all this and would really appreciate some feedback on the best way to do it.
The main error is that it does not save the XML name and what the XML file contains in its corresponding fields
p_text
orp_text_nar
- what are you expecting those to contain? Also why are looping and getting lines from the XML and storing them individually - don't you want the entire XML document stored as a single value?