1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

XML Parser for PL/SQL Example: PL/SQL — domsample.sql

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.26 MB, 774 trang )


Using XML Parser for PL/SQL Examples in the Sample Directory



dbms_output.put(xmldom.getNodeName(n) || ' ');

end loop;

dbms_output.put_line('');

end printElements;

-- prints the attributes of each element in a document

procedure printElementAttributes(doc xmldom.DOMDocument) is

nl xmldom.DOMNodeList;

len1 number;

len2 number;

n xmldom.DOMNode;

e xmldom.DOMElement;

nnm xmldom.DOMNamedNodeMap;

attrname varchar2(100);

attrval varchar2(100);

begin

-- get all elements

nl := xmldom.getElementsByTagName(doc, '*');

len1 := xmldom.getLength(nl);

-- loop through elements

for j in 0..len1-1 loop

n := xmldom.item(nl, j);

e := xmldom.makeElement(n);

dbms_output.put_line(xmldom.getTagName(e) || ':');

-- get all attributes of element

nnm := xmldom.getAttributes(n);

if (xmldom.isNull(nnm) = FALSE) then

len2 := xmldom.getLength(nnm);

-- loop through attributes

for i in 0..len2-1 loop

n := xmldom.item(nnm, i);

attrname := xmldom.getNodeName(n);

attrval := xmldom.getNodeValue(n);

dbms_output.put(' ' || attrname || ' = ' || attrval);

end loop;

dbms_output.put_line('');

end if;

end loop;



XML Parser for PL/SQL 20-11



Using XML Parser for PL/SQL Examples in the Sample Directory



end printElementAttributes;

begin

-- new parser

p := xmlparser.newParser;

-- set some characteristics

xmlparser.setValidationMode(p, FALSE);

xmlparser.setErrorLog(p, dir || '/' || errfile);

xmlparser.setBaseDir(p, dir);

-- parse input file

xmlparser.parse(p, dir || '/' || inpfile);

-- get document

doc := xmlparser.getDocument(p);

-- Print document elements

dbms_output.put('The elements are: ');

printElements(doc);

-- Print document element attributes

dbms_output.put_line('The attributes of each element are: ');

printElementAttributes(doc);

-- deal with exceptions

exception

when xmldom.INDEX_SIZE_ERR then

raise_application_error(-20120, 'Index Size error');

when xmldom.DOMSTRING_SIZE_ERR then

raise_application_error(-20120, 'String Size error');

when xmldom.HIERARCHY_REQUEST_ERR then

raise_application_error(-20120, 'Hierarchy request error');

when xmldom.WRONG_DOCUMENT_ERR then

raise_application_error(-20120, 'Wrong doc error');

when xmldom.INVALID_CHARACTER_ERR then

raise_application_error(-20120, 'Invalid Char error');



20-12 Oracle9i XML Developer’s Kits Guide - XDK



Using XML Parser for PL/SQL Examples in the Sample Directory



when xmldom.NO_DATA_ALLOWED_ERR then

raise_application_error(-20120, 'Nod data allowed error');

when xmldom.NO_MODIFICATION_ALLOWED_ERR then

raise_application_error(-20120, 'No mod allowed error');

when xmldom.NOT_FOUND_ERR then

raise_application_error(-20120, 'Not found error');

when xmldom.NOT_SUPPORTED_ERR then

raise_application_error(-20120, 'Not supported error');

when xmldom.INUSE_ATTRIBUTE_ERR then

raise_application_error(-20120, 'In use attr error');

end domsample;

/

show errors;



XML Parser for PL/SQL Example: PL/SQL — xslsample.sql

------



This file demonstates a simple use of XSLT transformation capabilities.

The XML and XSL files that are given to the application are parsed,

the transformation specified is applied and the transformed document is

written to a specified result file.

It shows you how to set the parser options.



set serveroutput on;

create or replace procedure xslsample(dir varchar2, xmlfile varchar2,

xslfile varchar2, resfile varchar2,

errfile varchar2) is

p xmlparser.Parser;

xmldoc xmldom.DOMDocument;

xmldocnode xmldom.DOMNode;

proc xslprocessor.Processor;

ss xslprocessor.Stylesheet;

xsldoc xmldom.DOMDocument;

docfrag xmldom.DOMDocumentFragment;

docfragnode xmldom.DOMNode;

xslelem xmldom.DOMElement;

nspace varchar2(50);

xslcmds xmldom.DOMNodeList;

begin



XML Parser for PL/SQL 20-13



Using XML Parser for PL/SQL Examples in the Sample Directory



-- new parser

p := xmlparser.newParser;

-- set some characteristics

xmlparser.setValidationMode(p, FALSE);

xmlparser.setErrorLog(p, dir || '/' || errfile);

xmlparser.setPreserveWhiteSpace(p, TRUE);

xmlparser.setBaseDir(p, dir);

-- parse xml file

dbms_output.put_line('Parsing XML document ' || dir || '/' || xmlfile);

xmlparser.parse(p, dir || '/' || xmlfile);

-- get document

xmldoc := xmlparser.getDocument(p);

-- parse xsl file

dbms_output.put_line('Parsing XSL document ' || dir || '/' || xslfile);

xmlparser.parse(p, dir || '/' || xslfile);

-- get document

xsldoc := xmlparser.getDocument(p);

xslelem := xmldom.getDocumentElement(xsldoc);

nspace := xmldom.getNamespace(xslelem);

-- print out some information about the stylesheet

dbms_output.put_line('XSL Root element information');

dbms_output.put_line('Qualified Name: ' ||

xmldom.getQualifiedName(xslelem));

dbms_output.put_line('Local Name: ' ||

xmldom.getLocalName(xslelem));

dbms_output.put_line('Namespace: ' || nspace);

dbms_output.put_line('Expanded Name: ' ||

xmldom.getExpandedName(xslelem));

xslcmds := xmldom.getChildrenByTagName(xslelem, '*', nspace);

dbms_output.put_line('A total of ' || xmldom.getLength(xslcmds) ||

' XSL instructions were found in the stylesheet');

-- make stylesheet

ss := xslprocessor.newStylesheet(xsldoc, dir || '/' || xslfile);

-- process xsl

proc := xslprocessor.newProcessor;

xslprocessor.showWarnings(proc, true);



20-14 Oracle9i XML Developer’s Kits Guide - XDK



Xem Thêm
Tải bản đầy đủ (.pdf) (774 trang)

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×