XML document into multiple tables
How to insert a xml document into multiple tables. Eg. Purchase Order having multiple line items. I have to insert xml document into Parent as well as child with different sets of columns.
I created the tables using the create_ch14_tables.sql. I call it using java -classpath .;C:\commerceone\xpc\lib\xmlparserv2.jar;C:\commerceone\xpc\lib\classes12.zip;C:\commerceone\xpc\lib\xsu12.jar XMLLoader -file deptempdepend.xml -connName default -transform deptempdepend.xsl. The code doesn't seem to like the "<xsl:for-each select="Department">" tags. If I remove them, the insDoc.getDocumentElement().getFirstChild() will find the element, but it still doesn't insert anything into the database.
Thank You,
Dave
Similar Messages
-
To load an XML document into 40 tables
How do I load a large XML document into 40 tables. Most of the exmaples, I see only load one table into the Orcale database?
From the above document:
Storing XML Data Across Tables
Question
Can XML- SQL Utility store XML data across tables?
Answer
Currently XML-SQL Utility (XSU) can only store to a single table. It maps a canonical representation of an XML document into any table/view. But of course there is a way to store XML with the XSU across table. One can do this using XSLT to transform any document into multiple documents and insert them separately. Another way is to define views over multiple tables (object views if needed) and then do the inserts ... into the view. If the view is inherently non-updatable (because of complex joins, ...), then one can use INSTEAD-OF triggers over the views to do the inserts.
-- I've tried this, works fine. -
How to save sections of a single XML Document to multiple tables ?
Firstly, I apologise for the long e-mail but I feel it's necessary in order to clarify my problem/question.
The XML document representation below stores information about a particular database. From the information in the XML document you can tell that there is a single database called "tst" which contains a single table called "tst_table". This table in turn has two columns called "CompanyName" & "Country".
I want to use Oracle's XML SQL Utility to store this information into three seperate database tables. Specifically, I want to store the information pertaining to the database (i.e. name etc.) in one table, the information pertaining to the table (name, no. of columns etc.) in another and the information pertaining to the columns (name, type etc.) in yet another table.
I have seen samples where an entire XML Document is saved to a database table but I cannot find any examples where different sections of a single XML Document are saved into different database tables using the XML SQL Utility.
Can you please tell me the best approach to take in order to accomplish this . Does it involve creating an XMLDocument and then extracting the relevant sections as XMLDocumentFragment's, retrieving the String representations of these XMLDocumentFragment's and passing these strings to the OracleXMLSave.insertXml() method.
Is this the best approach to take or are there any other, perhaps more efficient or elegant, ways of doing this ?
Thanks in advance for your help
- Garry
<DATABASE id="1" name="tst">
<TABLES>
<TABLE name="tst_table">
<NAME>Customers</NAME>
<COLUMNS>
<COLUMN num="1"> <COLID>2</COLID>
<COLNAME>CompanyName</COLNAME>
<COLTYPE>Text</COLTYPE>
</COLUMN>
<COLUMN num="2">
<COLID>3</COLID>
<COLNAME>Country</COLNAME>
<COLTYPE>Text</COLTYPE>
</COLUMN>
</COLUMNS>
</TABLE>
</TABLES>
</DATABASE>
nullSee this thread;
{thread:id=2180799}
Jeff -
Loading XML files into multiple tables
I've got XML like so...
<?xml version="1.0" encoding="UTF-8"?>
<MainTitle Version="1.0" Date="2009-01-11">
<MainName>
<ID1>A</ID1>
<ID2>ABC</ID2>
<ID3>ABC123</ID3>
<Desc>Some text</Desc>
<feature>f1</feature>
<feature>f2</feature>
<Category>
<name>n1</name>
<attribute>more stuff</attribute>
</Category>
<Category>
<name>n2</name>
<attribute>even more stuff</attribute>
</Category>
<Category>
<name>n3</name>
<attribute>different stuff</attribute>
</Category>
<Category>
<name>n4</name>
<attribute>More of the same<attribute>
<attribute>But different still</attribute>
<attribute>Even more different junk<attribute>
</Category>
</MainName>
</MainTitle>
Where each MainName instance in the file can have 0 or more ( unbounded ) Category and Feature tags and each Category instance can have multiple attribute tags. The file contains many thousands of MainName instances and has embedded a good mix of possible tags.
I believe I can load this into 9i xmltype table or a 9i table with an xmltype column, then query the data to get it out...
SQL> create table mytab (
2 xmlraw XMLType
3 );
Table created.
SQL>
SQL> insert into mytab values ( sys.xmltype.createxml(
2 '<?xml version="1.0" encoding="UTF-8"?>
3 <MainTitle Version="1.0" Date="2009-01-11">
4 <MainName>
5 <ID1>A</ID1>
6 <ID2>ABC</ID2>
7 <ID3>ABC123</ID3>
8 <Desc>Some text</Desc>
9 <feature>f1</feature>
10 <feature>f2</feature>
11 <Category>
12 <name>n1</name>
13 <attribute>more stuff</attribute>
14 </Category>
15 <Category>
16 <name>n2</name>
17 <attribute>even more stuff</attribute>
18 </Category>
19 <Category>
20 <name>n3</name>
21 <attribute>different stuff</attribute>
22 </Category>
23 <Category>
24 <name>n4</name>
25 <attribute>More of the same</attribute>
26 <attribute>But different still</attribute>
27 <attribute>Even more different junk</attribute>
28 </Category>
29 </MainName>
30 </MainTitle>')
31 );
1 row created.
1 select
2 extract(a.xmlraw,'/MainTitle/MainName/ID1/text()'),
3 extract(a.xmlraw,'/MainTitle/MainName/ID2/text()'),
4 extract(a.xmlraw,'/MainTitle/MainName/ID3/text()'),
5 extract(a.xmlraw,'/MainTitle/MainName/Desc/text()'),
6 extract(a.xmlraw,'/MainTitle/MainName/feature/text()'),
7 extract(a.xmlraw,'/MainTitle/MainName/Category/text()'),
8 extract(a.xmlraw,'/MainTitle/MainName/Category/name/text()'),
9 extract(a.xmlraw,'/MainTitle/MainName/Category/attribute/text()')
10* from mytab a
SQL> /
A
ABC
ABC123
Some text
f1f2
n1n2n3n4
more stuffeven more stuffdifferent stuffMore of the sameBut different stillEven
more different junk
This all works just fine, however, it's not quite what I need. For starters, the multiple tag data is concatenated and when I try to specifically query it out using a where clause I get ORA 22950. So, not sure how to deal with that.
Is it possible to use sqlldr to get the 200MB XML file loaded into a table like that above?
Now, given multiple "feature" and "category" data per "MainName", I need to use the SQL to dump the XML data into a set of tables built to model the structure of the XML...
roughly..
Main_Table (
ID1 Varchar2(10)
ID2 varchar2(10)
ID3 varchar2(10)
desc varchar2(100)
Features_Table (
ID1 varchar2(10)
feature varchar2(100)
Category_Table (
ID1 varchar2(10)
name varchar2(100)
attribute varchar2(100)
What are the groups recommendations here? Should I continue down this route or is there a better way?When I suggested the option to parse the XML in PL/SQL I was referring to pulling the data into PL/SQL and then performing all parsing activity against the PL/SQL copy and you don't need to make SQL calls.
Here is a quick sample for parsing out all the Category/name elements from the XML once it is loaded into PL/SQL
DECLARE
l_index PLS_INTEGER;
l_category XMLTYPE;
l_db_row XMLTYPE := XMLTYPE('<?xml version="1.0" encoding="UTF-8"?>
<MainTitle Version="1.0" Date="2009-01-11">
<MainName>
<ID1>A</ID1>
<ID2>ABC</ID2>
<ID3>ABC123</ID3>
<Desc>Some text</Desc>
<feature>f1</feature>
<feature>f2</feature>
<Category>
<name>n1</name>
<attribute>more stuff</attribute>
</Category>
<Category>
<name>n2</name>
<attribute>even more stuff</attribute>
</Category>
<Category>
<name>n3</name>
<attribute>different stuff</attribute>
</Category>
<Category>
<name>n4</name>
<attribute>More of the same</attribute>
<attribute>But different still</attribute>
<attribute>Even more different junk</attribute>
</Category>
</MainName>
</MainTitle>');
BEGIN
l_index := 1;
WHILE l_db_row.Existsnode('/MainTitle/MainName/Category[' ||
To_Char(l_index) || ']') > 0
LOOP
l_category := l_db_row.Extract('/MainTitle/MainName/Category[' ||
To_Char(l_index) || ']');
dbms_output.put_line(l_category.extract('Category/name/text()').getStringVal());
l_index := l_index + 1;
END LOOP;
END;You could repeat the WHILE loop to parse out the attribute column as well since it repeats. This is what Dave's post was showing and what I was referring to.
Hint: If you are trying to use .extract to go after an optional node, you need to verify the node exists via existsNode first. If you don't you can get an "ORA-30625: method dispatch on NULL SELF argument is disallowed" error when trying to extract a non-existent node. -
Loading xml data into eexternal table
emp.xml is xml file namewhich is saved in C:\Documents and Settings\james\Desktop\emp.xml
xml file
<EMPLOYEES>
<EMP>
<EMPNO>7369</EMPNO>
<ENAME>SMITH</ENAME>
<JOB>CLERK</JOB>
<HIREDATE>17-DEC-80</HIREDATE>
<SAL>800</SAL>
</EMP>
<EMP>
<EMPNO>7499</EMPNO>
<ENAME>ALLEN</ENAME>
<JOB>SALESMAN</JOB>
<HIREDATE>20-FEB-81</HIREDATE>
<SAL>1600</SAL>
<COMM>300</COMM>
</EMP>
</EMPLOYEES>
CREATE DIRECTORY my_xml_dir AS 'C:\Documents and Settings\james\Desktop\emp.xml'
CREATE TABLE my_xml_et ( EMPNO NUMBER, EMPNAME VARCHAR2(10), JOB VARCHAR2(10), HIREDATE DATE, SAL NUMBER )
using external tables how this xml data is loaded into my_xml_etI think you mean you are trying to load an XML from/using an external table.
However, external tables are typically best suited to flat file structures rather than XML nested structures.
What you require is one of the many techniques for shredding an XML document into oracle table structures.
More information on these techniques can be found in the XML DB forum, with plenty of useful links in the FAQ for that forum...
XML DB FAQ -
This is my first thread so I hope I have formatted my request appropriately...
If I have a simple XML doc with several records (see below) what is the best / easiest way to insert the data into multiple tables (Oracle 10g Release 1)?
For example 3 tables:
PATIENT table (CLAIMID, FNAME, LNAME, HEALTHNUM)
ADDRESS table (CLAIMID, STREET, CITY, PROVINCE)
SERVICE table (CLAIMID, SERVICE, ADMITDATE, DEPARTDATE)
<?xml version="1.0"?>
<CLAIMS>
<CLAIM>
<CLAIMID>1</CLAIMID>
<FNAME>Oscar</FNAME>
<LNAME>Grouch</LNAME>
<HEALTHNUM>1234657890</HEALTHNUM>
<STREET>123 Sesame Street</STREET>
<CITY>Sesameville</CITY>
<PROVINCE>ON</PROVINCE>
<SERVICE>Heart Surgery</SERVICE>
<ADMITDATE>2007-06-05</ADMITDATE>
<DEPARTDATE>2007-06-11</DEPARTDATE>
</CLAIM>
<CLAIM>
<CLAIMID>2</CLAIMID>
<FNAME>Poppa</FNAME>
<LNAME>Smurf</LNAME>
<HEALTHNUM>0987654321</HEALTHNUM>
<STREET>44 Blue Street</STREET>
<CITY>Smurfville</CITY>
<PROVINCE>ON</PROVINCE>
<SERVICE>Lung Transplant</SERVICE>
<ADMITDATE>2007-05-28</ADMITDATE>
<DEPARTDATE>2007-06-04</DEPARTDATE>
</CLAIM>
...more <CLAIM> records
</CLAIMS>
I can import the XML document into an XMLType column but don't know where to go from there...
The extractValue method seems to work only when there is a single claim record in the XML document but a typical document will contain several hundred claims.
Thanks in advance,
M KentUse the Oracle Berkeley DB XML to store the XML document in a embedded xml database. To store in the Oracle 10g database use XSU. With XSU an XML document may also be stored in multiple tables.
http://download-east.oracle.com/docs/cd/A97329_03/web.902/a88894/adx07xsu.htm#1016732 -
Collect data from a dynamic XML file into multiple internal tables
I need to convert the XML file into multiple internal tables. I tried many links and posts in SDN but still was facing difficulty in achieving this. Can some one tell me where I am going wrong.
My XML file is of the following type.It is very complex and the dynamice.
The following tags occur more than once in the XML file. The "I" and "L" tags and its child tags can occur ones or more than once for each XML file and it is not constant. i.e in one file they can occur 1 time and in another they can occur 100 times.
"I" and "L" are child tags of <C>
<I>
<J>10</J>
<K>EN</K>
</I>
<L>
<J>20</J>
<N>BB</N>
</L>
Tags <C> and <F> occur only ones for each XML file. <C> is the child tag of "A" and "F" is the child tag of <C>.
I need to collect <D>, <E> in one internal table ITAB.
I need to collect <G>, <H> in one internal table JTAB.
I need to collect <J>, <K> in one internal table KTAB.
I need to collect <J>, <N> in one internal table PTAB.
Below is the complete XML file.
?xml version="1.0" encoding="iso-8859-1" ?>
<A>
<B/>
<C>
<D>RED</D>
<E>999</E>
<F>
<G>TRACK</G>
<H>PACK</H>
</F>
<I>
<J>10</J>
<K>EN</K>
</I>
<I>
<J>20</J>
<K>TN</K>
</I>
<I>
<J>30</J>
<K>KN</K>
</I>
<L>
<J>10</J>
<N>AA</N>
</L>
<L>
<J>20</J>
<N>BB</N>
</L>
<L>
<J>30</J>
<N>CC</N>
</L>
</C>
</A>
With the help of SDN I am able to gather the values of <D> <E> in one internal table.
Now if I need to gather
<G>, <H> in one internal table JTAB.
<J>, <K> in one internal table KTAB.
<J>, <N> in one internal table PTAB.
I am unable to do. I am following XSLT transformation method. If some one has some suggestions. Please help.
Here is my ABAP program
TYPE-POOLS abap.
CONSTANTS gs_file TYPE string VALUE 'C:\TEMP\ABCD.xml'.
* This is the structure for the data from the XML file
TYPES: BEGIN OF ITAB,
D(10) TYPE C,
E(10) TYPE C,
END OF ITAB.
* Table for the XML content
DATA: gt_itab TYPE STANDARD TABLE OF char2048.
* Table and work ares for the data from the XML file
DATA: gt_ITAB TYPE STANDARD TABLE OF ts_ITAB,
gs_ITAB TYPE ts_ITAB.
* Result table that contains references
* of the internal tables to be filled
DATA: gt_result_xml TYPE abap_trans_resbind_tab,
gs_result_xml TYPE abap_trans_resbind.
* For error handling
DATA: gs_rif_ex TYPE REF TO cx_root,
gs_var_text TYPE string.
* Get the XML file from your client
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = gs_file
CHANGING
data_tab = gt_itab1
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18
OTHERS = 19.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* Fill the result table with a reference to the data table.
* Within the XSLT stylesheet, the data table can be accessed with
* "IITAB".
GET REFERENCE OF gt_shipment INTO gs_result_xml-value.
gs_result_xml-name = 'IITAB'.
APPEND gs_result_xml TO gt_result_xml.
* Perform the XSLT stylesheet
TRY.
CALL TRANSFORMATION zxslt
SOURCE XML gt_itab1
RESULT (gt_result_xml).
CATCH cx_root INTO gs_rif_ex.
gs_var_text = gs_rif_ex->get_text( ).
MESSAGE gs_var_text TYPE 'E'.
ENDTRY.
* Now let's see what we got from the file
LOOP AT gt_ITAB INTO gs_ITAB.
WRITE: / 'D:', gs_ITAB-D.
WRITE: / 'E :', gs_ITAB-E.
ENDLOOP.
Transformation
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<IITAB>
<xsl:apply-templates select="//C"/>
</IITAB>
</asx:values>
</asx:abap>
</xsl:template>
<item>
<D>
<xsl:value-of select="D"/>
</D>
<E>
<xsl:value-of select="E"/>
</E>
</item>
</xsl:template>
</xsl:transform>
Now the above pgm and transformation work well and I am able to extract data into the ITAB. Now what changes should I make in transformation and in pgm to collect
<G>, <H> in one internal table JTAB.
<J>, <K> in one internal table KTAB.
<J>, <N> in one internal table PTAB.
Please help..i am really tring hard to figure this out. I am found lot of threads addressing this issue but not my problem.
Kindly help.
Regards,
VSHi Rammohan,
Thanks for the effort!
But I don't need to use GUI upload because my functionality does not require to fetch data from presentation server.
Moreover, the split command advised by you contains separate fields...f1, f2, f3... and I cannot use it because I have 164 fields. I will have to split into 164 fields and assign the values back to 164 fields in the work area/header line.
Moreover I have about 10 such work areas. so the effort would be ten times the above effort! I want to avoid this! Please help!
I would be very grateful if you could provide an alternative solution.
Thanks once again,
Best Regards,
Vinod.V -
Need to Load data in XML file into multiple Oracle tables
Experts
I want to load data that is stored at XML file into multiple Oracle table using plsql.
I am novice in this area.Kindly explain in depth or step by step so that i can achive this.
Thanks in advnace.Hi,
extract your xml and then you can use insert all clause.
here's very small example on 10.2.0.1.0
SQL> create table table1(id number,val varchar2(10));
Table created.
SQL> create table table2(id number,val varchar2(10));
Table created.
SQL> insert all
2 into table1 values(id,val)
3 into table2 values(id2,val2)
4 select extractValue(x.col,'/a/id1') id
5 ,extractValue(x.col,'/a/value') val
6 ,extractValue(x.col,'/a/value2') val2
7 ,extractValue(x.col,'/a/id2') id2
8 from (select xmltype('<a><id1>1</id1><value>a</value><id2>2</id2><value2>b</value2></a>') col from dual) x;
2 rows created.
SQL> select * from table1;
ID VAL
1 a
SQL> select * from table2;
ID VAL
2 b Ants -
How to convert Xml Document into orcale tempary table
i am creating xml document into java and passing this xml into oracle database and i need to fetch this xml into result set | rowset.
Xml Structure
<Department deptno="100">
<DeptName>Sports</DeptName>
<EmployeeList>
<Employee empno="200"><Ename>John</Ename><Salary>33333</Salary>
</Employee>
<Employee empno="300"><Ename>Jack</Ename><Salary>333444</Salary>
</Employee>
</EmployeeList>
</Department>
i need like this format
Deptno DeptName empno Ename Salary
100 Sports 200 Jhon 2500
100 Sports 300 Jack 3000It does depend on your version as odie suggests.
Here's a way that will work in 10g...
SQL> ed
Wrote file afiedt.buf
1 with t as (select xmltype('<Department deptno="100">
2 <DeptName>Sports</DeptName>
3 <EmployeeList>
4 <Employee empno="200"><Ename>John</Ename><Salary>33333</Salary>
5 </Employee>
6 <Employee empno="300"><Ename>Jack</Ename><Salary>333444</Salary>
7 </Employee>
8 </EmployeeList>
9 </Department>
10 ') as xml from dual)
11 --
12 -- End of test data, Use query below
13 --
14 select x.deptno, x.deptname
15 ,y.empno, y.ename, y.salary
16 from t
17 ,xmltable('/'
18 passing t.xml
19 columns deptno number path '/Department/@deptno'
20 ,deptname varchar2(10) path '/Department/DeptName'
21 ,emps xmltype path '/Department/EmployeeList'
22 ) x
23 ,xmltable('/EmployeeList/Employee'
24 passing x.emps
25 columns empno number path '/Employee/@empno'
26 ,ename varchar2(10) path '/Employee/Ename'
27 ,salary number path '/Employee/Salary'
28* ) y
SQL> /
DEPTNO DEPTNAME EMPNO ENAME SALARY
100 Sports 200 John 33333
100 Sports 300 Jack 333444
SQL>If the XML is a string e.g. a CLOB then it can easily be converted to XMLTYPE using the XMLTYPE function. -
Insert Insert XML file into multiple records in Oracle Database
I would like to find out if it is possible to insert a single XML file into multiple records or tuples in a Oracle database table. I have a single XML file which is at multiple levels. The meta data for the levels are common and each level can have meta data of their own in addition. I do not have any meta data field which will uniquely determine whether the data belongs to Root level, Intermediate level or at the document level. Is there any way I can determine which level the meta data belongs to and thereby make a corresponding entry into the database table tuple? For example I could have an attribute called level which is going to be present only in the database table and not in the XML file. If level=1 then it corresponds to "Root" meta data, if level=2 then it corresponds to "Intermediate" level and if level=3 then it corresponds to meta data at document level. I need a way to calculate the value for level from the XML file and thereby insert the meta data element into a tuple in a single table in Oracle.
Hi,
extract your xml and then you can use insert all clause.
here's very small example on 10.2.0.1.0
SQL> create table table1(id number,val varchar2(10));
Table created.
SQL> create table table2(id number,val varchar2(10));
Table created.
SQL> insert all
2 into table1 values(id,val)
3 into table2 values(id2,val2)
4 select extractValue(x.col,'/a/id1') id
5 ,extractValue(x.col,'/a/value') val
6 ,extractValue(x.col,'/a/value2') val2
7 ,extractValue(x.col,'/a/id2') id2
8 from (select xmltype('<a><id1>1</id1><value>a</value><id2>2</id2><value2>b</value2></a>') col from dual) x;
2 rows created.
SQL> select * from table1;
ID VAL
1 a
SQL> select * from table2;
ID VAL
2 b Ants -
How to load a XML file into a table
Hi,
I've been working on Oracle for many years but for the first time I was asked to load a XML file into a table.
As an example, I've found this on the web, but it doesn't work
Can someone tell me why? I hoped this example could help me.
the file acct.xml is this:
<?xml version="1.0"?>
<ACCOUNT_HEADER_ACK>
<HEADER>
<STATUS_CODE>100</STATUS_CODE>
<STATUS_REMARKS>check</STATUS_REMARKS>
</HEADER>
<DETAILS>
<DETAIL>
<SEGMENT_NUMBER>2</SEGMENT_NUMBER>
<REMARKS>rp polytechnic</REMARKS>
</DETAIL>
<DETAIL>
<SEGMENT_NUMBER>3</SEGMENT_NUMBER>
<REMARKS>rp polytechnic administration</REMARKS>
</DETAIL>
<DETAIL>
<SEGMENT_NUMBER>4</SEGMENT_NUMBER>
<REMARKS>rp polytechnic finance</REMARKS>
</DETAIL>
<DETAIL>
<SEGMENT_NUMBER>5</SEGMENT_NUMBER>
<REMARKS>rp polytechnic logistics</REMARKS>
</DETAIL>
</DETAILS>
<HEADER>
<STATUS_CODE>500</STATUS_CODE>
<STATUS_REMARKS>process exception</STATUS_REMARKS>
</HEADER>
<DETAILS>
<DETAIL>
<SEGMENT_NUMBER>20</SEGMENT_NUMBER>
<REMARKS> base polytechnic</REMARKS>
</DETAIL>
<DETAIL>
<SEGMENT_NUMBER>30</SEGMENT_NUMBER>
</DETAIL>
<DETAIL>
<SEGMENT_NUMBER>40</SEGMENT_NUMBER>
<REMARKS> base polytechnic finance</REMARKS>
</DETAIL>
<DETAIL>
<SEGMENT_NUMBER>50</SEGMENT_NUMBER>
<REMARKS> base polytechnic logistics</REMARKS>
</DETAIL>
</DETAILS>
</ACCOUNT_HEADER_ACK>
For the two tags HEADER and DETAILS I have the table:
create table xxrp_acct_details(
status_code number,
status_remarks varchar2(100),
segment_number number,
remarks varchar2(100)
before I've created a
create directory test_dir as 'c:\esterno'; -- where I have my acct.xml
and after, can you give me a script for loading data by using XMLTABLE?
I've tried this but it doesn't work:
DECLARE
acct_doc xmltype := xmltype( bfilename('TEST_DIR','acct.xml'), nls_charset_id('AL32UTF8') );
BEGIN
insert into xxrp_acct_details (status_code, status_remarks, segment_number, remarks)
select x1.status_code,
x1.status_remarks,
x2.segment_number,
x2.remarks
from xmltable(
'/ACCOUNT_HEADER_ACK/HEADER'
passing acct_doc
columns header_no for ordinality,
status_code number path 'STATUS_CODE',
status_remarks varchar2(100) path 'STATUS_REMARKS'
) x1,
xmltable(
'$d/ACCOUNT_HEADER_ACK/DETAILS[$hn]/DETAIL'
passing acct_doc as "d",
x1.header_no as "hn"
columns segment_number number path 'SEGMENT_NUMBER',
remarks varchar2(100) path 'REMARKS'
) x2
END;
This should allow me to get something like this:
select * from xxrp_acct_details;
Statuscode status remarks segement remarks
100 check 2 rp polytechnic
100 check 3 rp polytechnic administration
100 check 4 rp polytechnic finance
100 check 5 rp polytechnic logistics
500 process exception 20 base polytechnic
500 process exception 30
500 process exception 40 base polytechnic finance
500 process exception 50 base polytechnic logistics
but I get:
Error report:
ORA-06550: line 19, column 11:
PL/SQL: ORA-00932: inconsistent datatypes: expected - got NUMBER
ORA-06550: line 4, column 2:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
and if I try to change the script without using the column HEADER_NO to keep track of the header rank inside the document:
DECLARE
acct_doc xmltype := xmltype( bfilename('TEST_DIR','acct.xml'), nls_charset_id('AL32UTF8') );
BEGIN
insert into xxrp_acct_details (status_code, status_remarks, segment_number, remarks)
select x1.status_code,
x1.status_remarks,
x2.segment_number,
x2.remarks
from xmltable(
'/ACCOUNT_HEADER_ACK/HEADER'
passing acct_doc
columns status_code number path 'STATUS_CODE',
status_remarks varchar2(100) path 'STATUS_REMARKS'
) x1,
xmltable(
'/ACCOUNT_HEADER_ACK/DETAILS'
passing acct_doc
columns segment_number number path 'SEGMENT_NUMBER',
remarks varchar2(100) path 'REMARKS'
) x2
END;
I get this message:
Error report:
ORA-19114: error during parsing the XQuery expression:
ORA-06550: line 1, column 13:
PLS-00201: identifier 'SYS.DBMS_XQUERYINT' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
ORA-06512: at line 4
19114. 00000 - "error during parsing the XQuery expression: %s"
*Cause: An error occurred during the parsing of the XQuery expression.
*Action: Check the detailed error message for the possible causes.
My oracle version is 10gR2 Express Edition
I do need a script for loading xml files into a table as soon as possible, Give me please a simple example for understanding and that works on 10gR2 Express Edition
Thanks in advance!The reason your first SQL statement
select x1.status_code,
x1.status_remarks,
x2.segment_number,
x2.remarks
from xmltable(
'/ACCOUNT_HEADER_ACK/HEADER'
passing acct_doc
columns header_no for ordinality,
status_code number path 'STATUS_CODE',
status_remarks varchar2(100) path 'STATUS_REMARKS'
) x1,
xmltable(
'$d/ACCOUNT_HEADER_ACK/DETAILS[$hn]/DETAIL'
passing acct_doc as "d",
x1.header_no as "hn"
columns segment_number number path 'SEGMENT_NUMBER',
remarks varchar2(100) path 'REMARKS'
) x2
returns the error you noticed
PL/SQL: ORA-00932: inconsistent datatypes: expected - got NUMBER
is because Oracle is expecting XML to be passed in. At the moment I forget if it requires a certain format or not, but it is simply expecting the value to be wrapped in simple XML.
Your query actually runs as is on 11.1 as Oracle changed how that functionality worked when 11.1 was released. Your query runs slowly, but it does run.
As you are dealing with groups, is there any way the input XML can be modified to be like
<ACCOUNT_HEADER_ACK>
<ACCOUNT_GROUP>
<HEADER>....</HEADER>
<DETAILS>....</DETAILS>
</ACCOUNT_GROUP>
<ACCOUNT_GROUP>
<HEADER>....</HEADER>
<DETAILS>....</DETAILS>
</ACCOUNT_GROUP>
</ACCOUNT_HEADER_ACK>
so that it is easier to associate a HEADER/DETAILS combination? If so, it would make parsing the XML much easier.
Assuming the answer is no, here is one hack to accomplish your goal
select x1.status_code,
x1.status_remarks,
x3.segment_number,
x3.remarks
from xmltable(
'/ACCOUNT_HEADER_ACK/HEADER'
passing acct_doc
columns header_no for ordinality,
status_code number path 'STATUS_CODE',
status_remarks varchar2(100) path 'STATUS_REMARKS'
) x1,
xmltable(
'$d/ACCOUNT_HEADER_ACK/DETAILS'
passing acct_doc as "d",
columns detail_no for ordinality,
detail_xml xmltype path 'DETAIL'
) x2,
xmltable(
'DETAIL'
passing x2.detail_xml
columns segment_number number path 'SEGMENT_NUMBER',
remarks varchar2(100) path 'REMARKS') x3
WHERE x1.header_no = x2.detail_no;
This follows the approach you started with. Table x1 creates a row for each HEADER node and table x2 creates a row for each DETAILS node. It assumes there is always a one and only one association between the two. I use table x3, which is joined to x2, to parse the many DETAIL nodes. The WHERE clause then joins each header row to the corresponding details row and produces the eight rows you are seeking.
There is another approach that I know of, and that would be using XQuery within the XMLTable. It should require using only one XMLTable but I would have to spend some time coming up with that solution and I can't recall whether restrictions exist in 10gR2 Express Edition compared to what can run in 10.2 Enterprise Edition for XQuery. -
"PLS-00172: string literal too long" When Writing XML file into a Table
Hi.
I'm using DBMS_XMLStore to get a XML file into a db table. See the example below, I'm using that for my PL/SQL format. Problem is that because there're too many XML elements that I use in "xmldoc CLOB:= ...", I get "PLS-00172: string literal too long" error.
Can someone suggest a workaround?
THANKS!!!
DECLARE
insCtx DBMS_XMLStore.ctxType;
rows NUMBER;
xmldoc CLOB :=
'<ROWSET>
<ROW num="1">
<EMPNO>7369</EMPNO>
<SAL>1800</SAL>
<HIREDATE>27-AUG-1996</HIREDATE>
</ROW>
<ROW>
<EMPNO>2290</EMPNO>
<SAL>2000</SAL>
<HIREDATE>31-DEC-1992</HIREDATE>
</ROW>
</ROWSET>';
BEGIN
insCtx := DBMS_XMLStore.newContext('scott.emp'); -- get saved context
DBMS_XMLStore.clearUpdateColumnList(insCtx); -- clear the update settings
-- set the columns to be updated as a list of values
DBMS_XMLStore.setUpdateColumn(insCtx,'EMPNO');
DBMS_XMLStore.setUpdateColumn(insCtx,'SAL');
DBMS_XMLStore.setUpdatecolumn(insCtx,'HIREDATE');
-- Now insert the doc.
-- This will only insert into EMPNO, SAL and HIREDATE columns
rows := DBMS_XMLStore.insertXML(insCtx, xmlDoc);
-- Close the context
DBMS_XMLStore.closeContext(insCtx);
END;
/You ask where am getting the XML doc. Well, am not
getting the doc itself.I either don't understand or I disagree. In your sample code, you're certainly creating an XML document-- your local variable "xmldoc" is an XML document.
DBMS_XMLSTORE package needs
to know the canonical format and that's what I
hardcoded. Again, I either don't understand or I disagree... DBMS_XMLStore more or less assumes the format of the XML document itself-- there's a ROWSET tag, a ROW tag, and a then whatever column tags you'd like. You can override what tag identifies a row, but the rest is pretty much assumed. Your calls to setUpdateColumn identifies what subset of column tags in the XML document you're interested in.
Later in code I use
DBMS_XMLStore.setUpdateColumn to specify which
columns are to be inserted.Agreed.
xmldoc CLOB :=
'<ROWSET>
<ROW num="1">
<KEY_OLD> Smoker </KEY_OLD>
<KEY_NEW> 3 </KEY_NEW>
<TRANSFORM> Specified </TRANSFORM>
<KEY_OLD> Smoker </KEY_OLD>
<VALUEOLD> -1 </VALUEOLD>
EW> -1 </VALUENEW>
<DESCRIPTION> NA </DESCRIPTION>
</ROW>
ROWSET>';This is your XML document. You almost certainly want to be reading this from the file system and/or have it passed in from another procedure. If you hard-code the XML document, you're limited to a 32k string literal, which is almost certainly causing the error you were reporting initially.
As am writing this I'm realizing that I'm doing this
wrong, because I do need to read the XML file from
the filesystem (but insert the columns
selectively)...What I need to come up with is a proc
that would grab the XML file and do inserts into a
relational table. The XML file will change in the
future and that means that all my 'canonical format'
code will be broken. How do I deal with anticipated
change? Do I need to define/create an XML schema in
10g if am just inserting into one relat. table from
one XML file?What does "The XML file will change in the future" mean? Are you saying that the structure of the XML document will change? Or that the data in the XML document would change? Your code should only need to change if the structure of the document changes, which should be exceptionally uncommon and would only be an issue if you're adding another column that you want to work with, which would necessitate code changes.
I found an article where the issue of changing XML
file is dealt by using a XSL file (that's where I'd
define the 'canonical format'), but am having a
problem with creating one, because the source XML is
screwed up in terms of the format:
it's not <x> blah </x>
<x2> blah </x2>
x2="blah" x3="blah> ...etc
Can you point me in the right direction, please?You can certainly use something like the DBMS_XSLProcessor package to transform whatever XML document you have into an XML document in an appropriate format for the XMLStore package and pass that transformed XML document into something like your sample procedure rather than defining the xmldoc local variable with a hard-coded document. Of course, you'd need to write appropriate XSL code to do the actual transform.
Justin -
Convert XML page into Internal Table
Hi,
I'm not sure if this post belongs here or not, but here goes.
I have a requirement to read in data from a xml page and save the contents into a ABAP internal table.
At the moment, I have extracted the contents of the XML page into a string using the IF_HTTP_CLIENT->RESPONSE->GET_CDATA method.
Now I underatand that SAP has XSLT documents that can be used to transform XML documents into ABAP Data Structures.
Can someone explain to me how this works? I have looked in SAP Help about the XSLT Editor, but it still looks like double dutch to me (no offence to the dutch people out there!).
Thanks,
Pat.You can use either XSLT programming or Simple Transformation to do that job. check out the following weblogs.
<a href="/people/tobias.trapp/blog/2005/05/04/xml-processing-in-abap-part-1">XML processing in ABAP Part 1</a>
<a href="/people/tobias.trapp/blog/2005/05/17/xml-processing-in-abap-part-2">XML processing in ABAP Part 2</a>
<a href="/people/tobias.trapp/blog/2005/05/20/xml-processing-in-abap-part-3">XML processing in ABAP Part 3</a>
<a href="/people/tobias.trapp/blog/2005/05/27/xml-processing-in-abap-part-4">XML processing in ABAP Part 4</a>
<a href="/people/tobias.trapp/blog/2005/06/15/xml-processing-in-abap--part-5">XML processing in ABAP Part 5</a>
Regards
Raja -
Loading XML file into DB Table
Hi
I m quite new to the loading XML file into database table.
It will be great if anyone could guide me to through.
Now,
i have an XML file which has to be loaded into the DB table.
what are the steps involved in doing this. How do i go from here ??
your help is greatly appriciated ???
Thank you so much!!
-ShashiOK - Although you really should read the XMLDB FAQ on this forum, here is some sample code of ONE of the ways of doing it
(there are multiple ways - and this is not the most simple one)
Based on Oracle 11gR1
-- sqlplus /nolog
clear screen
set termout on
set feed on
set lines 40
set long 10000000
set serveroutput on
set lines 100
set echo on
connect / as sysdba
col filename for a80
col xml for a80
-- Create schema “OTN”
drop user OTN cascade;
purge dba_recyclebin;
create user OTN identified by OTN;
grant dba, xdbadmin to OTN;
EXECUTE dbms_java.grant_permission( 'OTN', 'java.io.FilePermission','G:\OTN\xmlstore','read' );
prompt pause
pause
clear screen
-- Create directory
connect OTN/OTN;
show user
drop directory OTN_USE_CASE;
CREATE directory OTN_USE_CASE AS 'G:\OTN\xmlstore';
SELECT extract((XMLTYPE(bfilename('OTN_USE_CASE','ABANDA-20030407215829881GMT.xml'),NLS_CHARSET_ID('AL32UTF8'))),'*') AS "XML"
from dual;
prompt pause
pause
clear screen
-- Directory Listing - Tom Kyte
create global temporary table DIR_LIST
( filename varchar2(255) )
on commit delete rows
create or replace
and compile java source named "DirList"
as
import java.io.*;
import java.sql.*;
public class DirList
{public static void getList(String directory)
throws SQLException
{ File path = new File( directory );
String[] list = path.list();
String element;
for(int i = 0; i < list.length; i++)
{ element = list;
#sql { INSERT INTO DIR_LIST (FILENAME)
VALUES (:element) };
create or replace procedure get_dir_list( p_directory in varchar2 )
as language java
name 'DirList.getList( java.lang.String )';
prompt pause
pause
clear screen
-- The content of the global temporary table
exec get_dir_list( 'G:\OTN\xmlstore' );
select * from dir_list;
-- "COMMIT" will clear / truncate the global temporary table...
prompt pause
pause
clear screen
-- Combined: Reading XML content from multiple XML files
commit;
exec get_dir_list( 'G:\OTN\xmlstore' );
select * from dir_list where filename like '%.xml'
and rownum <= 10;
prompt pause
pause
clear screen
select extract((XMLTYPE(bfilename('OTN_USE_CASE',dl.filename),NLS_CHARSET_ID('AL32UTF8'))),'*') AS "XML"
from dir_list dl
where dl.filename like '%.xml' and rownum <= 2;
prompt pause
pause
clear screen
-- If you can select it you can insert it...
-- drop table OTN_xml_store purge;
create table OTN_xml_store of xmltype
xmltype store as binary xml
commit;
exec get_dir_list( 'G:\OTN\xmlstore' );
set time on timing on
insert into OTN_xml_store
select XMLTYPE(bfilename('OTN_USE_CASE',dl.filename),NLS_CHARSET_ID('AL32UTF8')) AS "XML"
from dir_list dl
where dl.filename like '%.xml';
set time off timing off
commit;
select count(*) from OTN_xml_store;
prompt pause
pause
clear screen
-- If you can select it you can create resources and files
set time on timing on
commit;
exec get_dir_list( 'G:\OTN\xmlstore' );
select count(*) from dir_list where filename like '%.xml';
set serveroutput on size 10000
DECLARE
XMLdoc XMLType;
res BOOLEAN;
v_foldername varchar2(4000) := '/public/OTN/';
cursor c1
is
select dl.filename FNAME
, XMLTYPE(bfilename('OTN_USE_CASE',dl.filename),NLS_CHARSET_ID('AL32UTF8')) XMLCONTENT
from dir_list dl
where dl.filename like '%.xml'
and rownum <= 100;
BEGIN
-- Create XDB repository Folder
if (dbms_xdb.existsResource(v_foldername))
then
dbms_xdb.deleteResource(v_foldername,dbms_xdb.DELETE_RECURSIVE_FORCE);
end if;
res:=DBMS_XDB.createFolder(v_foldername);
-- Create XML files in the XDB Repository
for r1 in c1
loop
if (DBMS_XDB.CREATERESOURCE(v_foldername||r1.fname, r1.xmlcontent))
then
dbms_output.put_line(v_foldername||r1.fname);
null;
else
dbms_output.put_line('Loop Exception :'||sqlerrm);
end if;
end loop;
EXCEPTION WHEN OTHERS THEN
dbms_output.put_line('Others Exception: '||sqlerrm);
END;
set time off timing off
commit;
prompt pause
pause
clear screen
-- FTP and HTTP
clear screen
prompt
prompt *** FTP - Demo ***
prompt
prompt pause
pause
host ftp
-- open localhost 2100
-- user OTN OTN
-- cd public
-- cd OTN
-- ls
-- bye
clear screen
prompt
prompt *** Microsoft Internet Explorer - Demo ***
prompt
prompt pause
pause
host "C:\Program Files\Internet Explorer\IEXPLORE.EXE" http://OTN:OTN@localhost:8080/public/OTN/
prompt pause
pause
-- Accessing the XDB Repository content via Resource View
-- Selecting content from a resource via XBDUriType
clear screen
prompt set long 300
set long 300
prompt Relative Path - (path)
SELECT path(1) as filename
FROM RESOURCE_VIEW
WHERE under_path(RES, '/public/OTN', 1) = 1
and rownum <= 10
prompt pause
pause
clear screen
prompt Absolute Path - (any_path)
select xdburitype(any_path).getClob() as xml
FROM RESOURCE_VIEW
WHERE under_path(RES, '/public/OTN', 1) = 1
and rownum <= 1;
prompt pause
pause
-- CLEANUP ENVIRONMENT
clear screen
prompt
prompt >>>>> Clean UP !!! <<<<<<
prompt
prompt Cleanup environment and drop user...!!!
prompt
pause
clear screen
conn / as sysdba
alter session set current_schema=OTN;
begin
dbms_xdb.deleteResource('/public/OTN',dbms_xdb.DELETE_RECURSIVE_FORCE);
commit;
end;
alter session set current_schema=sys;
drop user OTN cascade;
Based on http://www.liberidu.com/blog/?p=1053 -
Loading XML Data into Relational Table
Hello,
I receive an XML file generated from another tool (on Windows), I am trying to create a Linux shell script that will gather the needed XML file from my Linux database server, then have Oracle use this file to load the XML data into a relational table. This activity & data will be needed on an ongoing basis.
I have tried this two ways. First, I loaded the XML document into the database and tried to extract the data directly from the document, but that is not working. Now I want to try to read the data directly from the file on the server via select, however I am not getting any data returned. In the Select statement below, I am simply trying to query the data to see what is returned for my testing.
Create Table ci_results_table (transactionID Varchar2(100), //transactionID should be PrimaryKey but was getting NULL value errors during insert test, so removed PK
message Varchar2(200),
ci Varchar2(50),
processeddate xmltype,
status Varchar2(50),
sourcefile VarChar2(100));
select x.*
from XMLTable(
'TSPLoadResults/Results'
PASSING xmltype(bfilename('CMDB_DEVADHOCRESULTS_DIR','LoadResults-HP_146.results.xml'), nls_charset_id('AL32UTF8'))
COLUMNS
transactionID Varchar2(100) PATH 'TransactionID',
Result XMLType PATH 'Result',
Message Varchar2(200) PATH 'Message',
PrimaryKey Varchar2(50) PATH 'PrimaryKey',
ProcessedDate date PATH 'ProcessedDate',
Status Varchar2(50) PATH 'Status',
SourceFile VarChar2(100) PATH 'SourceFileName'
) x
Eventually I will need to build on this to limit the data returned to those records where SourceFileName is like 'HPDS%' and insert what is returned in to the ci_results_table. Attached is a sample Results XML file I am trying to load, It is named "ResultsTransformedtoUnix" because I used dos2Unix to convert it to Unix which may be right or wrong. (The output file I send out has to be transformed to DOS format before the other application can read it). Original file (before Unix conversion) named in script is also attached.
Please help. Thank you!Hi,
I see some wrong things in your query.
1) The obvious one, explaining why you're not getting any data : there's a typo in the XQuery expression, it's "Result" not "Results"
2) ProcessedDate cannot be extracted as a date (at least not directly) since it actually represents a timestamp, use TIMESTAMP WITH TIME ZONE datatype and cast back to DATE in the SELECT clause
3) transactionID is an attribute, it must be accessed with '@' (or 'attribute::' axis)
4) If the file encoding is truly ISO-8859-1 as the prolog suggests, then do not use AL32UTF8 but the corresponding charset name : WE8ISO8859P1
Here's the working query :
select x.transactionID
, x.Message
, x.Primarykey
, cast(x.ProcessedDate as date) ProcessDate
, x.Status
, x.SourceFile
from XMLTable(
'/TSPLoadResults/Result'
PASSING xmltype(bfilename('XML_DIR','LoadResults-HP_146.results.xml'), nls_charset_id('WE8ISO8859P1'))
COLUMNS
transactionID Varchar2(100) PATH '@transactionID',
Message Varchar2(200) PATH 'Message',
PrimaryKey Varchar2(50) PATH 'PrimaryKey',
ProcessedDate timestamp with time zone PATH 'ProcessedDate',
Status Varchar2(50) PATH 'Status',
SourceFile VarChar2(100) PATH 'SourceFileName'
) x
Using this query directly over the file will only perform decently (for large files) on 11.2.0.4 and onwards.
On prior versions, first load the file in a (temporary) XMLType column with Binary XML storage and SELECT from there.
because I used dos2Unix to convert it to Unix which may be right or wrong.
This conversion shouldn't be necessary.
Maybe you are looking for
-
'''''All I can do is send a Blk & Wht email with preset font and size. Also, ALL, 100% of the apps below the left column of folders are missing. Large files, photo editing, stationary etc. In fact the scroll stops at the bottom of the list of folders
-
Query problem(solved)
hi frs, i want to display work_location and office_location in a report.which is in table HR_LOCATIONS_ALL Which has two location_id work location : location_id from per_all_assignments_f & office location: location_id from hr_all_organization_units
-
Hello, Can someone help me transform this SQL query into a TopLink Expression : select attri1Tab1, attri2Tab1 from tab1 where attri1 like '45%' and tab1Id1 in (select tab2Id1 from tab2 where attri1Tab2 ='1'); --> where tab2Id1 is the foreign key to t
-
Populating Authorization value in Customer exit
Dear Experts, I have two info objects Sales org and Plant and both are hierarchies.And both are having the same values ie Sales org A100 = Plant A100 And Sales org hierarchy is like this: A100 ( Top Node) -A110 (Child of top node) --A111 (child of A1
-
I have made payment but unable to pay my skype pre...
Hi - I was notified that my payment instruction failed. I could add 25 dollars to the skype credit account. Dont know why the payment instruction failed. Now I want to use the skype credit to pay for the premium account renewal, there is no optio