Sub Zero Wins

Monday, September 14, 2009

Examples of parsing/creating xml and calling a webservice in plsql

Parsing an xml
=================

begin

-- soapresp is an xmltype

if soapResp is not null then
l_doc := xmldom.newDOMDocument(soapResp); end if;

--get all the elements
l_n_list := xmldom.getElementsByTagName(l_doc, '*');

--Initialize tables
l_taxtype := taxtype(null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null);
l_taxtbl := new taxtbl();
LineTaxesTbl := new taxtbl();

--dbms_output.put_line(xmldom.getlength(l_n_list));
for i in 0 .. xmldom.getlength(l_n_list) - 1 loop
l_node := xmldom.item(l_n_list, i);
--dbms_output.put_line(xmldom.getNodeName(l_node));
l_nodeName := xmldom.getNodeName(l_node);

if l_NodeName = 'LineItem' then

l_taxtbl.extend(1);
l_taxtype.Lineid := xmldom.getattribute(xmldom.MakeElement(l_node),
'lineItemId');
l_line_list := xmldom.getChildNodes(l_node);
for k in 0 .. xmldom.getlength(l_line_list) - 1 loop
l_lineNode := xmldom.item(l_line_list, k);
.
.
.
.
.
.
.
.



============================================================================

Calling a webservice from plsql
================================
create or replace procedure getTax(web_service IN VARCHAR2,
web_service_param IN VARCHAR2) IS
-- req demo_soap.request;
-- resp demo_soap.response;
a varchar2(32000);

http_req utl_http.req;
http_resp utl_http.resp;
doc xmltype;
x dbms_xmldom.DomDocument;

env varchar2(32767) := '' ||
chr(10) || ' ' ||
' ' ||
' admin vertex 1041 Old Cassatt Road Berwyn PA Chester 19312 USA product code value 1.0 0 1000 1000
' ||
'
';

-- ' admin vertex 1041 Old Cassatt Road Berwyn PA Chester 19312 USA product code value 1.0 0 1000 1000 ';
/* req.body := '' ||
chr(10) || ' ' || chr(10) ||
' ' || chr(10) ||
' admin vertex 1041 Old Cassatt Road Berwyn PA Chester 19312 USA product code value 1.0 0 1000 1000
' ||
chr(10) || '
';
*/
BEGIN

-- req.method := 'calculateTax50';
-- req.namespace := 'http://www.vertexinc.com/oseries/services/CalculateTax50';
-- req.body := ' admin vertex 1041 Old Cassatt Road Berwyn PA Chester 19312 USA product code value 1.0 0 1000 1000 ';

http_req := utl_http.begin_request('http://denwlsq1:8911/vertex-ws/services/CalculateTax50',
'POST',
'HTTP/1.0');
utl_http.set_header(http_req, 'Content-Type', 'text/xml');
utl_http.set_header(http_req, 'Content-Length', length(env));
utl_http.set_header(http_req, 'SOAPAction', '');
utl_http.write_text(http_req, env);
http_resp := utl_http.get_response(http_req);
utl_http.read_text(http_resp, a);
utl_http.end_response(http_resp);
doc := xmltype.createxml(a);
--dbms_output.put_line(doc.getStringVal);
insert into tmp values (doc);
commit;
dbms_output.put_line(doc.getstringval());

END;
/




=========================================
declare
-- xmlparser
p xmlparser.parser;
-- Document
doc xmldom.DOMDocument;
-- Elements
el xmldom.DOMElement;
-- Nodes
buffyNode xmldom.DOMNode;
docNode xmldom.DOMNode;
vampire_node xmldom.DOMNode;
demon_node xmldom.DOMNode;
enemy_node xmldom.DOMNode;
friend_node xmldom.DOMNode;
node xmldom.DOMNode;
t dbms_xmldom.DOMText;

--Attributes
attr xmldom.DOMAttr;
-- Varchar2
temp1 varchar2(10000);
temp2 varchar2(255);

BEGIN
p := xmlparser.newParser;
xmlparser.parseBuffer(p,'');
doc := xmlparser.getDocument(p);
xmldom.setVersion(doc, '1.0');
docNode := xmldom.makeNode(doc);
buffyNode := xmldom.getLastChild(docNode); --The last child is the quickbuy node
-- ok

el := xmldom.createElement(doc, 'Friend');
friend_node := xmldom.appendChild(buffyNode, xmldom.makeNode(el));
el := xmldom.createElement(doc, 'Enemy');
enemy_node := xmldom.appendChild(buffyNode, xmldom.makeNode(el));

--test

el := xmldom.createElement(doc , 'Pratest');
node := xmldom.MakeNode(el);
node := xmldom.appendChild(buffyNode,node);

t := xmldom.createTextNode(doc,'ttt');

node := xmldom.appendChild(node,xmldom.MakeNode(t));


--test


el := xmldom.createElement(doc, 'Vampire');
xmldom.setAttribute(el, 'name','Spike');
xmldom.setAttribute(el, 'danger','zero');
vampire_node := xmldom.appendChild(friend_node, xmldom.makeNode(el));

el := xmldom.createElement(doc, 'Vampire');
xmldom.setAttribute(el, 'name','Harmony Kendal');
xmldom.setAttribute(el, 'danger','high');
vampire_node := xmldom.appendChild(enemy_node, xmldom.makeNode(el));

el := xmldom.createElement(doc, 'Demon');
xmldom.setAttribute(el, 'name','Ovu Mobani');
xmldom.setAttribute(el, 'danger','high');
demon_node := xmldom.appendChild(enemy_node, xmldom.makeNode(el));

el := xmldom.createElement(doc, 'Human');
xmldom.setAttribute(el, 'name','Lunch Lady');
xmldom.setAttribute(el, 'danger','extreme');
demon_node := xmldom.appendChild(enemy_node, xmldom.makeNode(el));


xmldom.writeToBuffer(doc, temp1);
temp2 := substr(temp1,1,250);
DBMS_OUTPUT.PUT_LINE(temp2);

temp2 := substr(temp1,251,250);
DBMS_OUTPUT.PUT_LINE(temp2);


-- deal with exceptions
exception

when xmldom.INDEX_SIZE_ERR then
-- raise_application_error(--20120, 'Index Size error');
DBMS_OUTPUT.PUT_LINE('Index Size error');
when xmldom.DOMSTRING_SIZE_ERR then
-- raise_application_error(--20120, 'String Size error');
DBMS_OUTPUT.PUT_LINE('String Size error');
when xmldom.HIERARCHY_REQUEST_ERR then
-- raise_application_error(--20120, 'Hierarchy request error');
DBMS_OUTPUT.PUT_LINE('Hierarchy request error');
when xmldom.WRONG_DOCUMENT_ERR then
-- raise_application_error(--20120, 'Wrong doc error');
DBMS_OUTPUT.PUT_LINE('Wrong doc error');
when xmldom.INVALID_CHARACTER_ERR then
-- raise_application_error(--20120, 'Invalid Char error');
DBMS_OUTPUT.PUT_LINE('Invalid char error');
when xmldom.NO_DATA_ALLOWED_ERR then
-- raise_application_error(--20120, 'Nod data allowed error');
DBMS_OUTPUT.PUT_LINE('No data allowed error');
when xmldom.NO_MODIFICATION_ALLOWED_ERR then
-- raise_application_error(--20120, 'No mod allowed error');
DBMS_OUTPUT.PUT_LINE('No mode allowed error');
when xmldom.NOT_FOUND_ERR then
-- raise_application_error(--20120, 'Not found error');
DBMS_OUTPUT.PUT_LINE('Not found error');
when xmldom.NOT_SUPPORTED_ERR then
-- raise_application_error(--20120, 'Not supported error');
DBMS_OUTPUT.PUT_LINE('Not supported error');
when xmldom.INUSE_ATTRIBUTE_ERR then
-- raise_application_error(--20120, 'In use attr error');
DBMS_OUTPUT.PUT_LINE('In use attr error');
END;




==========================================

DECLARE
dom_doc dbms_xmldom.DOMDocument;
n_doc dbms_xmldom.DOMNode;
e dbms_xmldom.DOMElement;
n dbms_xmldom.DOMNode;
n_directory dbms_xmldom.DOMNode;
n_temp dbms_xmldom.DOMNode;
t dbms_xmldom.DOMText;
buf VARCHAR2(2000);
BEGIN
-- New Document
dom_doc := dbms_xmldom.newDOMDocument;
dbms_xmldom.setVersion( dom_doc, '1.0' );
n_doc := dbms_xmldom.makeNode( dom_doc );
--
-- Create Root Element.
e := dbms_xmldom.createElement( dom_doc, 'description' );
dbms_xmldom.setAttribute( e, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance' );
n := dbms_xmldom.makeNode( e );
n_directory := dbms_xmldom.appendChild( n_doc, n );
--
-- Create description_id element.
e := dbms_xmldom.createElement( dom_doc, 'description_id' );
n := dbms_xmldom.makeNode( e );
n := dbms_xmldom.appendChild( n_directory, n );
t := dbms_xmldom.createTextNode( dom_doc, '456' );
n := dbms_xmldom.appendChild( n, dbms_xmldom.makeNode( t ) );
--
-- Create anatomical_notes element
e := dbms_xmldom.createElement( dom_doc, 'anatomical_notes' );
n := dbms_xmldom.makeNode( e );
n := dbms_xmldom.appendChild( n_directory, n );
t := dbms_xmldom.createTextNode( dom_doc, 'anatomical notes & works' );
n := dbms_xmldom.appendChild( n, dbms_xmldom.makeNode( t ) );
--
-- make node.
--DBMS_OUTPUT.PUT_LINE( dbms_xmldom.getNodeName( n_d ) );
--DBMS_OUTPUT.PUT_LINE( dbms_xmldom.getNodeValue( n_d ) );
--DBMS_OUTPUT.PUT_LINE( dbms_xmldom.getNodeType( n_d ) );
--e := dbms_xmldom.getDocumentElement( d );
--n := dbms_xmldom.makenode( e );
--DBMS_OUTPUT.PUT_LINE( dbms_xmldom.ELEMENT_NODE );
dbms_xmldom.writetobuffer( n_doc, buf );
--DBMS_OUTPUT.PUT_LINE( dbms_xmldom.getDoctype( d ) );
DBMS_OUTPUT.PUT_LINE(buf);
EXCEPTION
--
WHEN xmldom.INDEX_SIZE_ERR THEN
-- raise_application_error(--20120, 'Index Size error');
DBMS_OUTPUT.PUT_LINE('Index Size error');
WHEN xmldom.DOMSTRING_SIZE_ERR THEN
-- raise_application_error(--20120, 'String Size error');
DBMS_OUTPUT.PUT_LINE('String Size error');
WHEN xmldom.HIERARCHY_REQUEST_ERR THEN
-- raise_application_error(--20120, 'Hierarchy request error');
DBMS_OUTPUT.PUT_LINE('Hierarchy request error');
WHEN xmldom.WRONG_DOCUMENT_ERR THEN
-- raise_application_error(--20120, 'Wrong doc error');
DBMS_OUTPUT.PUT_LINE('Wrong doc error');
WHEN xmldom.INVALID_CHARACTER_ERR THEN
-- raise_application_error(--20120, 'Invalid Char error');
DBMS_OUTPUT.PUT_LINE('Invalid char error');
WHEN xmldom.NO_DATA_ALLOWED_ERR THEN
-- raise_application_error(--20120, 'Nod data allowed error');
DBMS_OUTPUT.PUT_LINE('No data allowed error');
WHEN xmldom.NO_MODIFICATION_ALLOWED_ERR THEN
-- raise_application_error(--20120, 'No mod allowed error');
DBMS_OUTPUT.PUT_LINE('No mode allowed error');
WHEN xmldom.NOT_FOUND_ERR THEN
-- raise_application_error(--20120, 'Not found error');
DBMS_OUTPUT.PUT_LINE('Not found error');
WHEN xmldom.NOT_SUPPORTED_ERR THEN
-- raise_application_error(--20120, 'Not supported error');
DBMS_OUTPUT.PUT_LINE('Not supported error');
WHEN xmldom.INUSE_ATTRIBUTE_ERR THEN
-- raise_application_error(--20120, 'In use attr error');
DBMS_OUTPUT.PUT_LINE('In use attr error');
END;
/