Solved

PL SQL example for procedure to get XML, Update it and return.

Posted on 2003-11-04
5
1,636 Views
Last Modified: 2012-06-27
Hi, Experts.
I need to write some procedure that will receive XML, analyze/parse it (take the Table Name from my MetaData table by using TableID that XML give me ) and return the Value of this table by using the code and Table name (Update the Value attribute in the incomming XML).

I have a MetaData Table like this:
__________________________________________
TableID    |  TableName   |   TableField   | Value  |
__________________________________________
1             |  Table1          |   Field1         | 1000   |
1             |  Table1          |   Field8         | 2345   |
2             |  Table2          |   Field2         | 2000   |
3             |  Table3          |   Field3         | 3000   |
3             |  Table12        |   Field2         | 23123  |
-----------------------------------------------------------

The TableID and TableField fields are PK for me.

The lookup table named Table1 looks like this:
___________________________
Field1   |  Field2  | ... |  Field8  |
--------------------------------------
1000    | 1236     | ... |  2345    |
--------------------------------------

The in comming XML Example is:
<?xml version="1.0" encoding="UTF-8"?>
<root>
      <Record TableID="1" TableField="Field1" Value=""/>
      <Record TableID="2" TableField="Field2" Value=""/>
      <Record TableID="3" TableField="Field3" Value=""/>
</root>

The Returned XML should be like this:
The in comming XML Example is:
<?xml version="1.0" encoding="UTF-8"?>
<root>
      <Record TableID="1" TableField="Field1" Value="1000"/>
      <Record TableID="2" TableField="Field2" Value="2000"/>
      <Record TableID="3" TableField="Field3" Value="3000"/>
</root>

I am just selecting the Value=1000 from Table1 and Field1 and Update the XML.
Please, help me.
I need the exact PL SQL select statement to do this.

This question is important me and very urgent!!!
(500 Points - It wiil be graded as the Best = 500 x4)
Thanks, RRR.
0
Comment
Question by:RRR
5 Comments
 
LVL 3

Author Comment

by:RRR
ID: 9679651
Some correction:
The Value field in not exists In MetaData table.

Thanks,
RRR.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 9680456
you'll probably be better off asking this in the ORACLE database Topic Area,
0
 
LVL 21

Accepted Solution

by:
oleggold earned 500 total points
ID: 10605711
You can try something like this:

1. test case for Your tables:
 begin
  for i in 1..10
  loop
 insert into meta_tabs
 (tableid, tablename, tablefield, value)
  values
  (i, 'TABLE'||i, 'FIELD'||i,i+i*1000);
  end loop;
  end;

You can use an intermediate table to store Your XML
create table MetaData (col1 xmltype);
then use the following procedure to insert Your XML:

procedure InsXML(xmlDoc IN CLOB, tableName IN VARCHAR2,
PBATCH_SIZE IN PLS_INTEGER DEFAULT 7777)
is    
insCtx DBMS_XMLSave.ctxType;    
rows number;  
begin    
insCtx := DBMS_XMLSave.newContext(tableName); -- get the context handle
   DBMS_XMLSave.Setbatchsize(insCtx,PBATCH_SIZE);
   DBMS_XMLSave.Setrowtag(insCtx,XMLROWS_TAG);
rows := DBMS_XMLSave.insertXML(insCtx,xmlDoc); -- this inserts the

document    
dbms_output.put_line(to_char(rows) || ' rows inserted');
writelog(000, to_char(rows) || ' rows inserted');  
DBMS_XMLSave.closeContext(insCtx);            -- this closes the handle
end InsXML;

then the following function will be used from

fetch_xml_val_F  to parse the values and append fields values to :
CREATE OR REPLACE FUNCTION xml_dataF( p_sql         in varchar2,
                           p_max_rows in number )
               RETURN CLOB
                            is
         l_ctx    dbms_xmlquery.ctxType;
         l_xml    clob;
       begin
         dbms_lob.createtemporary( l_xml, true, dbms_lob.session );
         l_ctx := dbms_xmlquery.newContext( p_sql );

         -- setup the context
         dbms_xmlquery.setRaiseNoRowsException( l_ctx,true );
         dbms_xmlquery.setMaxRows( l_ctx, p_max_rows );

         -- get the xml fm the context
         l_xml := dbms_xmlquery.getXML( l_ctx );

         -- print out the xml result set
       --  print_xml( l_xml );

         -- determine the no of rows processed
         dbms_output.put_line( '' );
         dbms_output.put_line( 'No. of rows processed: ' ||
                               dbms_xmlquery.getnumrowsprocessed( l_ctx ) );

         dbms_xmlquery.closeContext(l_ctx);
         return l_xml;
       exception
         when others then
         dbms_xmlquery.closeContext(l_ctx);
       end xml_dataF;
and finaly the getting proc:
create or replace procedure fetch_xml_val_pF
 is
p_xml CLOB;
REC_XML clob;
         l_xml  varchar2(30000);
         l_line varchar2(2000);
         l_amt  pls_integer := 30000;
         l_idx  pls_integer := 1;
       begin
  select t.col1.extract('//root/Record/TableID/text()text()') || ',' ||
  t.col1.extract('//root/Record TableID/text()')
  into  p_xml
 from METADATA t;  
 REC_XML:=xml_dataF(p_sql => 'select value from meta_tabs',p_max_rows => l_idx);
         for i in 1 .. ceil(dbms_lob.getlength(p_xml)/l_amt) loop
           l_xml := dbms_lob.substr(p_xml, l_amt, l_idx);
                     loop
           exit when l_xml is null;
   l_line := substr(l_xml, 1, instr(l_xml, chr(10))-1);
   dbms_output.put_line('|' || l_line);
                       
          l_xml := substr(l_xml, instr(l_xml, chr(10)||REC_XML)+1);
           

           end loop;
         end loop;
       end ;
/
 







the XML output:
ALSO CAN BE USED WITH CLOB PARAMETERS
declare
v_xml clob:='<?xml version="1.0" encoding="UTF-8"?>
<root>
     <Record TableID="1" TableField="Field1" Value=""/>
     <Record TableID="2" TableField="Field2" Value=""/>
     <Record TableID="3" TableField="Field3" Value=""/>
</root>
';
begin
-- select sys.xmldom.(m.col1) into v_xml from metadata m;
  -- Call the procedure
  fetch_xml_val(p_xml => v_xml,
                 ptab=>'META_TABS');
end;

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now