Building Server-Side XML Schema Validation



Similar documents
SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

Programming Database lectures for mathema

Continuous Integration Part 2

3.GETTING STARTED WITH ORACLE8i

Developing Stored Procedures In Java TM. An Oracle Technical White Paper April 1999

Database Programming with PL/SQL: Learning Objectives

Building Web Applications, Servlets, JSP and JDBC

Developing SQL and PL/SQL with JDeveloper

PROCEDURES, FUNCTIONS AND PACKAGES

An Introduction to PL/SQL. Mehdi Azarmi

JDBC (Java / SQL Programming) CS 377: Database Systems

Oracle PL/SQL Injection

Oracle Database: Program with PL/SQL

Oracle8/ SQLJ Programming

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: Program with PL/SQL

Modern PL/SQL Code Checking and Dependency Analysis

Oracle Database: Program with PL/SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle For Beginners Page : 1

Oracle Database: SQL and PL/SQL Fundamentals

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

5. CHANGING STRUCTURE AND DATA

DEVELOPING CONTRACT - DRIVEN WEB SERVICES USING JDEVELOPER. The purpose of this tutorial is to develop a java web service using a top-down approach.

Oracle Database 10g: Program with PL/SQL

CS346: Database Programming.

Oracle Database: Program with PL/SQL

Calling Java from PL/SQL

FileMaker 14. ODBC and JDBC Guide

14 Triggers / Embedded SQL

Oracle Database: Program with PL/SQL

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

1 Triggers. 2 PL/SQL Triggers

SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

Oracle Data Redaction is Broken

Oracle Database: Develop PL/SQL Program Units

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

Oracle SQL. Course Summary. Duration. Objectives

Maintaining Stored Procedures in Database Application

Oracle Database 10g Express

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Geodatabase Programming with SQL

Oracle For Beginners Page : 1

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Exploiting PL/SQL Injection on Oracle 12c. with only. CREATE SESSION privileges

Writing Control Structures

Java SE 8 Programming

1 File Processing Systems

Real SQL Programming 1

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

A basic create statement for a simple student table would look like the following.

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Advanced SQL Injection in Oracle databases. Esteban Martínez Fayó

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

Database Extension 1.5 ez Publish Extension Manual

What is a database? COSC 304 Introduction to Database Systems. Database Introduction. Example Problem. Databases in the Real-World

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

Chain of Command Design Pattern & BI Publisher. Miroslav Samoilenko. Claremont is a trading name of Premiertec Consulting Ltd

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Working With Derby. Version 10.2 Derby Document build: December 11, 2006, 7:06:09 AM (PST)

Oracle For Beginners Page : 1

Oracle to MySQL Migration

Oracle For Beginners Page : 1

Microsoft SQL Server Features that can be used with the IBM i

Managing Objects with Data Dictionary Views. Copyright 2006, Oracle. All rights reserved.

This guide specifies the required and supported system elements for the application.

Virtual Private Database Features in Oracle 10g.

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

<Insert Picture Here> What's New in NetBeans IDE 7.2

Migrating Critical Business Applications to APEX Successfully

5.1 Database Schema Schema Generation in SQL

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

PL/SQL MOCK TEST PL/SQL MOCK TEST I

HP Quality Center. Upgrade Preparation Guide

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

Getting Started with Web Applications

Oracle Database 12c: Introduction to SQL Ed 1.1

Using SQL Developer. Copyright 2008, Oracle. All rights reserved.

Introduction to the Oracle DBMS

Oracle Database: SQL and PL/SQL Fundamentals NEW

High Performance XML Data Retrieval

J j enterpririse. Oracle Application Express 3. Develop Native Oracle database-centric web applications quickly and easily with Oracle APEX

Oracle PL/SQL Language. CIS 331: Introduction to Database Systems

Introduction to Triggers using SQL

Hacking and Protecting Oracle DB. Slavik Markovich CTO, Sentrigo

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Supplement IV.C: Tutorial for Oracle. For Introduction to Java Programming By Y. Daniel Liang

Talend for Data Integration guide

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

<Insert Picture Here> Oracle Application Express 4.0

TECH TUTORIAL: EMBEDDING ANALYTICS INTO A DATABASE USING SOURCEPRO AND JMSL

Oracle Database. Products Available on the Oracle Database Examples Media. Oracle Database Examples. Examples Installation Guide 11g Release 2 (11.

Oracle 10g PL/SQL Training

COMS20700 DATABASES 13 PL/SQL. COMS20700 Databases Dr. Essam Ghadafi

Transcription:

Building Server-Side XML Schema Validation pdf help August 2001 With the advent of XML Schema there is the requirement to validate XML documents against their associated schemas.this article gives an introduction to the XML schema validation process with the XDK for Java and discusses how to build an Oracle Java Stored Procedure to perform the schema validation on the server-side of the Oracle Database. The included sample also demonstrates the deployment procedure for Java Stored Procedures. content Create the Java Classes for XML Schema Validation Loading and Resolving the Java Class Publishing the Java Class by Defining the Specification Example Using the Stored Procedures XML Schema Validation can provide a flexible and portable form of data validation Resources for use in your applications. You may implement the XML validation process in your client-side or mid-tier applications but if you want either: Absolute control of data validation whenever the data is updatedinserted Make use of the data management capability of the Oracle database Then putting your data validation process inside a trigger or your PLSQL procedures on the server-side is a good solution. Since there is not a built-in PLSQL API to do XML Schema validation, we can create one using Java Stored Procedures. The first step in building a Java Stored Procedure for XML Schema validation is to select the components and decide the environment requirements. The components you'll need are: XML Schema Processor for Java [xschema.jar] XML Parser for Java [xmlparserv2.jar] Both of these are part of Oracle XML Developer s Kits for Java. See the Resource section to download the latest version of XDK for Java. The Oracle Database (8.1.6 version and above) is also needed as these versions fully support Java Stored Procedures. If you download the XDK for Java and have an Oracle 8.1.6 Database or above, you can follow the following steps to build up the Java Stored Procedure and take advantage of XML Schema for data validation. Create the Java Class with the Schema Validation Function Load and resolve the Java Class into Oracle Database Publish the Java by defining the specification Create the Java Classes for XML Schema Validation To build the Java Class for XML Schema Validation, two XDK packages, XML Schema Processor and XML Parser are needed: import oracle.xml.parser.schema.*; import oracle.xml.parser.v2.*; To be able to accept the inputs from PLSQL, we need another package import oracle.sql.char; You need to set xmlparserv2.jar, xschema.jar and classes12.zip to the CLASSPATH. The JDBC library classes12.zip is for JDK 1.2.x. If you are using JDK 1.1.x, classes111.zip is required to be used. The SchemaUtil Class is shown below: public class SchemaUtil file:d Jinyuinfopm_infodocumentxdksample_093001.htm (1 of 6) [1032001 2:42:40 PM]

Building Server-Side XML Schema Validation public static String validation(char xml, CHAR xsd) throws Exception Build Schema Object XSDBuilder builder = new XSDBuilder(); byte [] docbytes = xsd.getbytes(); ByteArrayInputStream in = new ByteArrayInputStream(docbytes); XMLSchema schemadoc = (XMLSchema)builder.build(in,null); Parse the input XML document with Schema Validation docbytes = xml.getbytes(); in = new ByteArrayInputStream(docbytes); DOMParser dp = new DOMParser(); Set Schema Object for Validation dp.setxmlschema(schemadoc); dp.setvalidationmode(xmlparser.schema_validation); dp.setpreservewhitespace (true); StringWriter sw = new StringWriter(); dp.seterrorstream (new PrintWriter(sw)); try dp.parse (in); sw.write("the input XML parsed without errors.\n"); catch (XMLParseException pe) sw.write("parser Exception: " + pe.getmessage()); catch (Exception e) sw.write("nonparserexception: " + e.getmessage()); return sw.tostring(); This class defines a single method, validation, which does the XML Schema validation for the input XML document and returns the error messages. To compile the class, use following command line: javac SchemaUtil.java This produces the compiled Java class, SchemaUtil.class. file:d Jinyuinfopm_infodocumentxdksample_093001.htm (2 of 6) [1032001 2:42:40 PM]

Loading and Resolving the Java Class With the utility loadjava, you can upload the Java source, class, and resource files into an Oracle database, where they are stored as Java schema objects. You can run loadjava from the command line or from an application, and you can specify several options including a resolver. Make sure you have $ORALCE_HOME\bin in your System Path to be able to run loadjava. Before loading the SchemUtil.class into the Database, we need to check if the correct version of the two dependent XDK Packages are loaded into the logon database schema (in this case xdkdemoxdkdemo). connect xdkdemoxdkdemo To check the status of the oracle.xml.parser.v2.domparser class, you can use the following SQL statement: SELECT SUBSTR(dbms_java.longname(object_name),1,35) AS class, status FROM all_objects WHERE object_type = 'JAVA CLASS' AND object_name = dbms_java.shortname('oraclexmlparserv2domparser'); If you see the result: CLASS STATUS ------------------------------------------------------- oraclexmlparserv2domparser VALID then the Oracle XML Parser for Java is already installed and ready to be used. If you see the above result, but the status is INVALID, try the command: ALTER JAVA CLASS _oraclexmlparserv2domparser Resolve If the verification procedure produces the SQL*Plus message no rows selected, you need to load the XML Parser into Database by: loadjava -resolve -verbose -user xdktempxdktemp xmlparserv2.jar If the parser is installed, then you don t need to complete any further installation steps. The SQL command for status checking will be: SELECT SUBSTR(dbms_java.longname(object_name),1,35) AS class, status FROM all_objects WHERE object_type = 'JAVA CLASS' AND object_name = dbms_java.shortname('oraclexmlparserschemaxmlschema'); Before loading the SchemaUtil.class, make sure that the loaded XML Parser has the same version with which you compiled the SchemaUtil.class. The following code can be used to check the current version of the loaded Oracle XML Parser: CREATE OR REPLACE FUNCTION XMLVersion RETURN VARCHAR2 IS LANGUAGE JAVA NAME 'oracle.xml.parser.v2.xmlparser.getreleaseversion() returns java.lang.string'; CREATE OR REPLACE Procedure getxmlversion AS begin dbms_output.put_line(xmlversion()); end; file:d Jinyuinfopm_infodocumentxdksample_093001.htm (3 of 6) [1032001 2:42:40 PM]

Then by issuing the command: SQL> set serveroutput on SQL> exec getxmlversion; You should receive the following result: Oracle XDK Java 9.0.2.0.0A Beta If the version doesn t match, you need to drop the package and reload it. To drop the package, you can issue following command line: dropjava -verbose -user xdktempxdktemp xmlparserv2.jar xschema.jar Once all of the versions are synced, we can finally load the SchemaUtil.class by: loadjava -resolve -verbose -user xdktempxdktemp SchemaUtil.class Publishing the Java Class by Defining the Specification For each Java method callable from SQL, you must write a call specification in Java, which exposes the method's top-level entry point to the Oracle server. CREATE OR REPLACE FUNCTION SchemaValidation(xml IN VARCHAR2,xsd IN VARCHAR2) return varchar2 IS LANGUAGE JAVA NAME 'SchemaUtil.validation(oracle.sql.CHAR,oracle.sql.CHAR) returns java.lang.string'; Now the Java stored procedure specification is created, both SQL and PLSQL can call it as if it were PLSQL function. Example Using the Stored Procedures You can call Java stored procedures from SQL DML statements, PLSQL blocks, and PLSQL subprograms. Using the SQL CALL statement, you can also call them from the top level (from SQL*Plus, for example) and from database triggers. The following example shows how to do XML Schema Validation using the created Java stored procedure. 1. Creating a Database Schema to store XML and XML Schema Documents. create table schema_tab(id number, xsd VARCHAR2(4000)); create table xml_tab(id number, xml VARCHAR2(4000)); 2. Loading the XML Schema Document into the Database You can just use the SQL commands to insert the data show in DBData.sql, like: Insert into schema_tab(1, [XML schema] ); 3. Calling the Java Stored Procedure to Validate the input XML Document inside the trigger of the xml_tab table -- Write XML Buffer to Output CREATE OR REPLACE PROCEDURE printbufferout(xmlstr IN OUT NOCOPY VARCHAR2) AS BEGIN line VARCHAR2(20000); nlpos INTEGER; LOOP file:d Jinyuinfopm_infodocumentxdksample_093001.htm (4 of 6) [1032001 2:42:40 PM]

EXIT WHEN xmlstr is null; nlpos := instr(xmlstr,chr(10)); line := substr(xmlstr,1,nlpos-1); -- print line IF(length(line) <250) THEN dbms_output.put_line(' ' line); ELSE dbms_output.put(' '); LOOP EXIT WHEN line is null; dbms_output.put_line(substr(line,1,250)); line := substr(line,250+1); END loop; END if; xmlstr := substr(xmlstr,nlpos+1); IF (nlpos = 0) THEN dbms_output.put_line(' ' xmlstr); EXIT; END if; END LOOP; END printbufferout; show errors; CREATE OR REPLACE PROCEDURE dbvalid(xmlid IN NUMBER, xsdid IN NUMBER) IS p_xml varchar2(4000); p_xsd varchar2(4000); p_out varchar2(4000); begin select xml into p_xml from xml_tab where id=xmlid; select xsd into p_xsd from schema_tab where id=xsdid; p_out := SchemaValidation(p_xml,p_xsd); printbufferout(p_out); end; For the date with the xdksample_093001.zipyou can execute the command and get following result: SQL> exec dbvalid(1,1); The input XML parsed without errors. PLSQL procedure successfully completed. SQL> exec dbvalid(2,1); <Line 5, Column 42>: XSD-2023: (Error) Invalid value of attribute: '1999-11-31' <Line 21, Column 27>: XSD-2105: (Error) Identity constraint validation error: file:d Jinyuinfopm_infodocumentxdksample_093001.htm (5 of 6) [1032001 2:42:40 PM]

'Key sequence not found in key reference'. Parser Exception: Invalid value of attribute: '1999-11-31' PLSQL procedure successfully completed. You can now use this Java Stored Procedure to Validate the XML document using PLSQL. Resources Download XML Developers Kit for Java from ORACLE Technology Network: http:technet.oracle.comtechxmlxdkhome.html Source code for the demo: xdksample_093001.zip file:d Jinyuinfopm_infodocumentxdksample_093001.htm (6 of 6) [1032001 2:42:40 PM]